簡體   English   中英

NSTextField 如何設置 NSAttributedString 的 TextStyle 屬性

[英]NSTextField How to Set NSAttributedString's TextStyle property

我正在嘗試以編程方式創建一個具有NSFont.TextStyle.headline字體樣式的 NSTextField。 此字體也可以從 storyboard 的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

我們需要獲取與文本樣式關聯的對應字體。

將這些常量傳遞給preferredFont(forTextStyle:options:) 或preferredFontDescriptor(forTextStyle:options:) 以檢索相應的字體或字體描述符。

所以:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM