我需要在我的ios项目中编写一个自定义字体选择器视图。当用户单击字体家族名称时,我需要显示特定家族的字体,我在姓氏中使用了名称作为特定的文本样式。我无法找到获得特定名称的文本样式名称的标准方法。请帮我解决这个问题。
例如,我已经比较了打印所有字体名称和UIFontPickerViewController ios组件字体名称。如果我打印姓氏和字体名称,它将在下面查找Times New Roman家族。
Family name Times New Roman
Font name: TimesNewRomanPS-ItalicMT
Font name: TimesNewRomanPS-BoldItalicMT
Font name: TimesNewRomanPS-BoldMT
Font name: TimesNewRomanPSMT但是在UIFontPickerViewController (ios组件)中,它的显示方式如下

我想知道,我们如何从上面打印的名字中得到文本样式的 Regular, Italic, Bold, Bold Italic?请帮助我,感谢你的反馈。请参考下面的源代码进行比较。
import UIKit
class ViewController: UIViewController, UIFontPickerViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let familyNames = UIFont.familyNames.sorted()
for family in familyNames {
print("Family name " + family)
let fontNames = UIFont.fontNames(forFamilyName: family)
for font in fontNames {
print(" Font name: " + font)
}
}
let configuration = UIFontPickerViewController.Configuration()
configuration.includeFaces = true
configuration.displayUsingSystemFont = true
configuration.filteredTraits = [.classModernSerifs]
let vc = UIFontPickerViewController(configuration: configuration)
vc.delegate = self
present(vc, animated: true)
}
}发布于 2022-10-31 15:27:36
我也有同样的问题,答案很难找到,因为我应该把它称为字体"face“,而不是字体”样式“。
无论如何,以下是在Swift 5.7中获取字体外观/字体样式名称的当前方法:
font.fontDescriptor.object(forKey: .face) as? String我为UIFont创建了一个小扩展,用于返回字体外观/样式。
public extension UIFont {
/// Gets the font style (face) name
var fontStyle: String {
return fontFace
}
/// Gets the font face name
var fontFace: String {
return (fontDescriptor.object(forKey: .face) as? String) ?? fontDescriptor.postscriptName
}
}https://stackoverflow.com/questions/63166921
复制相似问题