【问题标题】:NSTextField How to Set NSAttributedString's TextStyle propertyNSTextField 如何设置 NSAttributedString 的 TextStyle 属性
【发布时间】:2021-07-22 12:33:09
【问题描述】:

我正在尝试以编程方式创建一个具有NSFont.TextStyle.headline 字体样式的 NSTextField。也可以从情节提要的font 下拉菜单中设置此字体。

但是,当我执行以下操作时:

let attributes: [NSAttributedString.Key : Any] = [NSAttributedString.Key.font : NSFont.TextStyle.headline]
let attributedString = NSAttributedString(string: product.url, attributes: attributes)
let textLabel = NSTextField(labelWithAttributedString: attributedString)

我的应用程序崩溃并出现以下错误:

-[__NSCFConstantString pointSize]: unrecognized selector sent to instance 0x7fff8064f918

【问题讨论】:

标签: swift macos cocoa nstextfield


【解决方案1】:

我们需要获取与文本样式关联的对应字体。

将这些常量传递给preferredFont(forTextStyle:options:) 或preferredFontDescriptor(forTextStyle:options:) 以检索相应的字体或字体描述符。

因此:

let attributes: [NSAttributedString.Key : Any] = [NSAttributedString.Key.font : NSFont.preferredFont(forTextStyle: NSFont.TextStyle.headline, options: [:])]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-29
    • 1970-01-01
    • 2012-12-06
    相关资源
    最近更新 更多