2017-04-19 86 views
1

WatchKit支持using a monospaced font for digits。 使用Xcode 8.3.2和WatchOS 3.確定WKInterfaceLabel的字體大小和重量 - 需要寬字體

我相信我不能在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

不幸的是,沒有方法返回字體名稱,大小。另外,您也無法獲取標籤的當前文本。希望手錶的未來版本允許更多的API來做這些事情,因爲在可用版本中似乎有很多限制。

對於這樣那樣的要求,我也創建常量和管理的字體大小和名稱。您可以創建常量,枚舉,結構,plist,無論您的偏好如何。

+0

感謝雙重檢查Parth。 – wivku