如何根据当前的Locale在SwiftUI中为文本提供两种不同的字体?

huangapple go评论57阅读模式
英文:

How can i provide two different fonts to a text based on the current Locale in SwiftUI?

问题

I'm trying to create an app that will support two languages, the first one is English and the second one is the Kurdish language which uses letters that are similar to Arabic letters (ا،ب،ج). I want to give a text a specific font based on the selected locale of the app, for example, if the selected language is English I want the app to have the default font supplied by apple and if the locale is Kurdish I will supply my custom font. I know how to load a custom font and assign it individually to a specific text but I want to create some sort of a modifier that will handle the locality logic and apply the appropriate font based on the locale instead of checking for the locale in every text I create. here is a snippet code of what I'm trying to achieve:

Text("Test")
   .font(Font.myTitle())

Text will be "test" in English and will be "تێست" Kurdish.

I tried to extend the font to read from the environment variable but I guess I was wrong since environment variables are available only in views. here is what I tried in the extension:

extension Font{
    public static func myTitle() -> Font{
        @Environment(\.locale) var locale: Locale
        let isKurdish = locale.identifier == "ku"
        if(isKurdish) {
            return Font.custom("rabar_013", size: 30)
        }
        return Font.title
    }
}

Can anyone provide a solution to this problem?

英文:

I'm trying to create an app that will support two languages, the first one is English and the second one is the Kurdish language which uses letters that are similar to Arabic letters (ا،ب،ج). I want to give a text a specific font based on the selected locale of the app, for example, if the selected language is English I want the app to have the default font supplied by apple and if the locale is Kurdish I will supply my custom font. I know how to load a custom font and assign it individually to a specific text but I want to create some sort of a modifier that will handle the locality logic and apply the appropriate font based on the locale instead of checking for the locale in every text I create. here is a snippet code of what I'm trying to achieve:

Text("Test")
   .font(Font.myTitle())

Text will be "test" in English and will be "تێست" Kurdish.

I tried to extend the font to read from the environment variable but I guess I was wrong since environment variables are available only in views. here is what I tried in the extension:

extension Font{
    public static func myTitle() -> Font{
        @Environment(\.locale) var locale: Locale
        let isKurdish = locale.identifier == "ku"
        if(isKurdish) {
            return Font.custom("rabar_013", size: 30)
        }
        return Font.title
    }
}

Can anyone provide a solution to this problem?

答案1

得分: 0

以下是您要翻译的内容:

"Instead of using an extension on Font you can create your own ViewModifier that will have access to the Environment.

Something like this should do it.

struct LanguageTitleModifier: ViewModifier {
    @Environment(\.locale) private var locale

    func body(content: Content) -> some View {
        content
            .font(font)
    }

    private var font: Font {
        let isKurdish = locale.identifier == "ku"
        if isKurdish {
            return Font.custom("rabar_013", size: 30)
        } else {
            return Font.title
        }
    }
}

You can then create a helper extension on View so that it is easier to use.

extension View {
    func languageTitle() -> some View {
        self.modifier(LanguageTitleModifier())
    }
}

You then use it in the following way:

struct ContentView: View {
    var body: some View {
        Text("Hello, World!")
            .languageTitle()
    }
}
```"

<details>
<summary>英文:</summary>

Instead of using an extension on `Font` you can create your own `ViewModifier` that will have access to the `Environment`.

Something like this should do it.

struct LanguageTitleModifier: ViewModifier {
@Environment(.locale) private var locale

func body(content: Content) -&gt; some View {
    content
        .font(font)
}

private var font: Font {
    let isKurdish = locale.identifier == &quot;ku&quot;
    if isKurdish {
        return Font.custom(&quot;rabar_013&quot;, size: 30)
    } else {
        return Font.title
    }
}

}


You can then create a helper extension on `View` so that it is easier to use.

extension View {
func languageTitle() -> some View {
self.modifier(LanguageTitleModifier())
}
}


You then use it in the following way:

struct ContentView: View {
var body: some View {
Text("Hello, World!")
.languageTitle()
}
}



</details>



huangapple
  • 本文由 发表于 2023年1月9日 04:53:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75051213.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定