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)
問:確實沒有辦法獲得標籤的當前尺寸和重量和/或避免提供這些?
感謝雙重檢查Parth。 – wivku