确定WKInterfaceLabel的字体大小和重量 - 等宽字体所需

时间:2017-04-19 21:06:20

标签: swift watchkit

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)

问题:绝对没有办法获得标签的当前尺寸和重量和/或避免提供这些标签?

1 个答案:

答案 0 :(得分:1)

不幸的是,没有返回字体名称,大小的方法。此外,您也无法获取标签的当前文本。希望手表的未来版本允许更多API执行操作,因为可用版本似乎存在很多限制。

对于这种要求,我确实创建了常量并管理了字体大小和名称。您可以根据自己的喜好创建常量,枚举,结构,plist。

相关问题