【发布时间】:2015-06-29 10:47:56
【问题描述】:
有没有办法在 WatchKit 中为 San Francisco 字体设置等宽?在介绍新系统字体时,演示者仅针对 AppKit 显示。
欢迎提供示例代码。
Xcode:7.0 测试版 WatchOS:2.0
【问题讨论】:
标签: ios objective-c swift watchkit xcode7
有没有办法在 WatchKit 中为 San Francisco 字体设置等宽?在介绍新系统字体时,演示者仅针对 AppKit 显示。
欢迎提供示例代码。
Xcode:7.0 测试版 WatchOS:2.0
【问题讨论】:
标签: ios objective-c swift watchkit xcode7
Swift 版本:
let monospacedFont = UIFont.monospacedDigitSystemFontOfSize(16, weight: UIFontWeightRegular)
let monospacedString = NSAttributedString(string: "1234", attributes: [NSFontAttributeName: monospacedFont])
textLabel.setAttributedText(monospacedString)
【讨论】:
示例代码:
UIFont *systemFont = [UIFont systemFontOfSize:20];
UIFontDescriptor *monospacedNumberFontDescriptor = [systemFont.fontDescriptor fontDescriptorByAddingAttributes: @{
UIFontDescriptorFeatureSettingsAttribute: @[@{
UIFontFeatureTypeIdentifierKey: @6,
UIFontFeatureSelectorIdentifierKey: @0
}]
}];
UIFont *monospacedNumberSystemFont = [UIFont fontWithDescriptor:monospacedNumberFontDescriptor size:0];
NSMutableAttributedString *monospacedString = [[NSMutableAttributedString alloc] initWithString:@"1234567890"];
[monospacedString addAttribute:NSFontAttributeName value:monospacedNumberSystemFont range:NSMakeRange(0, monospacedString.string.length)];
[label setAttributedText:monospacedString];
【讨论】:
斯威夫特 4
extension WKInterfaceLabel {
func setMonospacedText(_ text: String) {
let monospacedFont = UIFont.monospacedDigitSystemFont(ofSize: 16, weight: UIFont.Weight.regular)
let attributedString = NSAttributedString(string: text, attributes: [NSAttributedStringKey.font: monospacedFont])
self.setAttributedText(attributedString)
}
}
【讨论】:
Apple Watch 需要使用SF Compact..
Apple Watch 使用 San Francisco Compact 的两种变体:显示屏 和文本。这些字体是专门为便于阅读而设计的 小屏幕。
有关更多信息,请参阅此link. 的Typeface 部分
【讨论】: