如何在 WatchKit 中使用等宽字体

How to use a monospaced font in WatchKit

在我的 WatchKit 布局中,我需要使用等宽字体。我是否必须添加等宽字体或使用系统字体有什么技巧吗?

您需要手动添加。 Apple 在编程指南中有一些很好的说明 here。此外,这里还有一些其他线程,其中包含更多故障排除细节。

  • Setting the font in a WKInterfaceLabel

您可以使用具有等宽变体的系统字体 (San Fransisco)。

对于WKInterfaceLabel,这在interface builder中不是一个可见的选项,你不能访问font 属性,所以你必须使用属性字符串来设置它:

UIFont *monospacedFont = [UIFont monospacedDigitSystemFontOfSize:36.0 weight:UIFontWeightRegular];
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:@"12:34:56"
                                                                       attributes:@{NSFontAttributeName: monospacedFont}];
[self.label setAttributedText:attributedString];