gpt4 book ai didi

swift - 确定 WKInterfaceLabel 的字体大小和粗细 - 等宽字体需要

转载 作者:可可西里 更新时间:2023-11-01 02:05:08 26 4
gpt4 key购买 nike

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)

问题:肯定没有办法获取标签的当前尺寸和重量和/或避免必须提供这些?

最佳答案

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

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

关于swift - 确定 WKInterfaceLabel 的字体大小和粗细 - 等宽字体需要,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43506058/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com