【问题标题】:determine font size and weight of WKInterfaceLabel - needed for monospaced font确定 WKInterfaceLabel 的字体大小和粗细 - 等宽字体需要
【发布时间】: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)

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

【问题讨论】:

    标签: swift watchkit


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2011-10-21
      • 2014-06-09
      • 2021-04-12
      • 2012-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-20
      • 2021-06-06
      相关资源
      最近更新 更多