iOS 自动使用自定义 Arabic/Cyrillic 字体
iOS using custom Arabic/Cyrillic fonts automatically
我正在尝试了解是否可以使用自定义阿拉伯语和西里尔语字体而无需对用户的语言设置进行 switch/if-else
。
我可以在应用程序中成功使用我的自定义字体。我想以同样的方式提供自定义 Ar/Cy 字体,而且我知道我可以将其构建到应用程序中。如果我有我的字体 SpecialFont.otf
并且还提供 SpecialFont-CY.otf
当用户使用西里尔语言时 OS 怎么知道使用 SpecialFontCY.otf?理想情况下,OS 将知道用户的主要字体,并且能够 select 一种字体,matches/includes 是该语言的正确字形。
PS。这是不是关于如何使用自定义字体的问题,我可以做到。我想知道如何在不编写这样的代码的情况下为各种语言提供多种字体以完全支持世界:
if NSLocale.preferredLanguages.first == "Arabic"
let myFont = UIFont(name:"SpecialFont-AR", size: 17)
else if NSLocale.preferredLanguages.first == "Russian"
let myFont = UIFont(name:"SpecialFont-CY", size: 17)
...etc
您需要以某种方式检查它以确定要设置的正确语言。
如果不想使用if/else
语法,可以使用三元条件运算符.
let myFont = (NSLocale.preferredLanguages.first == "Arabic") ? UIFont(name:"SpecialFont-AR", size: 17) : UIFont(name:"SpecialFont-CY", size: 17)
或者更具可读性,像这样:
let fontName = (NSLocale.preferredLanguages.first == "Arabic") ? "SpecialFont-AR" : "SpecialFont-CY"
let myFont = UIFont(name: fontName, size: 17)
不可以,但是您可以为 DRY 您的代码定义一个简单的扩展:
extension UIFont {
static func preferred(ofSize size: CGFloat) -> UIFont{
switch NSLocale.preferredLanguages.first {
case "Arabic": return UIFont(name:"SpecialFont-AR", size: size)!
case "Russian": return UIFont(name:"SpecialFont-CY", size: size)!
default: return UIFont.systemFont(ofSize: size) // etc.
}
}
}
现在您所要做的就是:
let myFont = UIFont.preferred(ofSize: 17)
您需要的不是 UIFont,而是 UIFontDescriptor。有了它,您可以设置字体属性 cascadeList
,它会根据字形可用性告诉系统 select 字体的顺序(即查看 SpecialFont,但如果找不到 Ø 的字形,请尝试SpecialFont-CY,然后是 SpecialFont-AR)。
级联列表的要点是 select 给定字形的正确字体。这样,如果一个字符串包含混合在一起的西里尔文、阿拉伯文和拉丁文,它仍然可以正常工作。
例如:
// Start with your base font
let font = UIFont(name:"SpecialFont", size: 17)!
// Create the ordered cascade list.
let cascadeList = [
UIFontDescriptor(fontAttributes: [.name: "SpecialFont-AR"]),
UIFontDescriptor(fontAttributes: [.name: "SpecialFont-CY"]),
]
// Create a new font descriptor based on your existing font, but adding the cascade list
let cascadedFontDescriptor = font.fontDescriptor.addingAttributes([.cascadeList: cascadeList])
// Make a new font base on this descriptor
let cascadedFont = UIFont(descriptor: cascadedFontDescriptor, size: font.pointSize)
这在 Creating Apps for a Global Audience(WWDC 2018)中有详细介绍。
我正在尝试了解是否可以使用自定义阿拉伯语和西里尔语字体而无需对用户的语言设置进行 switch/if-else
。
我可以在应用程序中成功使用我的自定义字体。我想以同样的方式提供自定义 Ar/Cy 字体,而且我知道我可以将其构建到应用程序中。如果我有我的字体 SpecialFont.otf
并且还提供 SpecialFont-CY.otf
当用户使用西里尔语言时 OS 怎么知道使用 SpecialFontCY.otf?理想情况下,OS 将知道用户的主要字体,并且能够 select 一种字体,matches/includes 是该语言的正确字形。
PS。这是不是关于如何使用自定义字体的问题,我可以做到。我想知道如何在不编写这样的代码的情况下为各种语言提供多种字体以完全支持世界:
if NSLocale.preferredLanguages.first == "Arabic"
let myFont = UIFont(name:"SpecialFont-AR", size: 17)
else if NSLocale.preferredLanguages.first == "Russian"
let myFont = UIFont(name:"SpecialFont-CY", size: 17)
...etc
您需要以某种方式检查它以确定要设置的正确语言。
如果不想使用if/else
语法,可以使用三元条件运算符.
let myFont = (NSLocale.preferredLanguages.first == "Arabic") ? UIFont(name:"SpecialFont-AR", size: 17) : UIFont(name:"SpecialFont-CY", size: 17)
或者更具可读性,像这样:
let fontName = (NSLocale.preferredLanguages.first == "Arabic") ? "SpecialFont-AR" : "SpecialFont-CY"
let myFont = UIFont(name: fontName, size: 17)
不可以,但是您可以为 DRY 您的代码定义一个简单的扩展:
extension UIFont {
static func preferred(ofSize size: CGFloat) -> UIFont{
switch NSLocale.preferredLanguages.first {
case "Arabic": return UIFont(name:"SpecialFont-AR", size: size)!
case "Russian": return UIFont(name:"SpecialFont-CY", size: size)!
default: return UIFont.systemFont(ofSize: size) // etc.
}
}
}
现在您所要做的就是:
let myFont = UIFont.preferred(ofSize: 17)
您需要的不是 UIFont,而是 UIFontDescriptor。有了它,您可以设置字体属性 cascadeList
,它会根据字形可用性告诉系统 select 字体的顺序(即查看 SpecialFont,但如果找不到 Ø 的字形,请尝试SpecialFont-CY,然后是 SpecialFont-AR)。
级联列表的要点是 select 给定字形的正确字体。这样,如果一个字符串包含混合在一起的西里尔文、阿拉伯文和拉丁文,它仍然可以正常工作。
例如:
// Start with your base font
let font = UIFont(name:"SpecialFont", size: 17)!
// Create the ordered cascade list.
let cascadeList = [
UIFontDescriptor(fontAttributes: [.name: "SpecialFont-AR"]),
UIFontDescriptor(fontAttributes: [.name: "SpecialFont-CY"]),
]
// Create a new font descriptor based on your existing font, but adding the cascade list
let cascadedFontDescriptor = font.fontDescriptor.addingAttributes([.cascadeList: cascadeList])
// Make a new font base on this descriptor
let cascadedFont = UIFont(descriptor: cascadedFontDescriptor, size: font.pointSize)
这在 Creating Apps for a Global Audience(WWDC 2018)中有详细介绍。