Dil*_*ara 7 uifont ios swift5 uifontpickerviewcontroller
我需要在我的 ios 项目中编写一个自定义字体选择器视图。当用户单击字体系列名称时,我需要显示特定系列的字体,我使用名称作为系列名称中的特定文本样式。我找不到获取特定名称的文本样式名称的标准方法。请帮我解决这个问题。
作为一个例子,我比较了打印所有字体名称和UIFontPickerViewControllerios 组件字体名称。如果我打印家族名称和字体名称,它会在下面查找Times New Roman家族。
Family name Times New Roman
Font name: TimesNewRomanPS-ItalicMT
Font name: TimesNewRomanPS-BoldItalicMT
Font name: TimesNewRomanPS-BoldMT
Font name: TimesNewRomanPSMT
Run Code Online (Sandbox Code Playgroud)
但在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)
}
}
Run Code Online (Sandbox Code Playgroud)
我有同样的问题,而且很难找到答案,因为我应该将其称为字体“face”而不是字体“style”。
无论如何,这是在 Swift 5.7 中获取字体/字体样式名称的当前方法:
font.fontDescriptor.object(forKey: .face) as? String
Run Code Online (Sandbox Code Playgroud)
我创建了一个小扩展来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
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2214 次 |
| 最近记录: |