【发布时间】:2017-04-20 20:28:15
【问题描述】:
WatchKit 支持using a monospaced font for digits。 使用 Xcode 8.3.2 和 WatchOS 3。
我相信我无法在 Interface Builder 中定义/选择它,因此我需要手动设置它。为此,我可以使用:
class func monospacedDigitSystemFont(ofSize fontSize: CGFloat,
weight: CGFloat) -> UIFont
它需要我设置尺寸和重量。我想从 Interface Builder 中定义的标签设置中继承这些。
但是:如何获取标签的当前尺寸和重量?似乎没有获取当前属性的方法。因此,在应用等宽字体时,我似乎需要对其进行硬编码。
@IBOutlet var myLabel: WKInterfaceLabel!
extension WKInterfaceLabel {
func setTextMono(_ str: String, size: CGFloat? = 15.0, weight: CGFloat? = UIFontWeightRegular) {
let monospacedFont = UIFont.monospacedDigitSystemFont(ofSize: size!, weight: weight!)
let attributedString = NSAttributedString(string: str, attributes: [NSFontAttributeName : monospacedFont])
self.setAttributedText(attributedString)
}
}
myLabel.setTextMono("12:13.14", size: 32.0, weight: UIFontWeightRegular)
问题:绝对没有办法获得标签的当前尺寸和重量和/或避免提供这些?
【问题讨论】: