我一直在创建一个 MacOS 应用程序,但在设置 NSSearchField(名为 searchField)的字体时遇到了问题。到目前为止,我的代码如下:
在单个主 viewController 类的顶部声明:
let normalTextStyle = NSFont(name: "PT Mono", size: 14.0)
let backgroundColour = NSColor(calibratedHue: 0.6,
saturation: 0.5,
brightness: 0.2,
alpha: 1.0)
let normalTextColour = NSColor(calibratedHue: 0.5,
saturation: 0.1,
brightness: 0.9,
alpha: 1.0)
在 viewDidLoad 中声明:
searchField.backgroundColor = backgroundColour
searchField.textColor = normalTextColour
searchField.font = normalTextStyle
searchField.centersPlaceholder = false
searchField.currentEditor()?.font = normalTextStyle
let attrStr = NSMutableAttributedString(string: "Search...",
attributes: [NSForegroundColorAttributeName: normalTextColour])
searchField.placeholderAttributedString = attrStr
通常,这在一种情况下有效:当搜索字段具有焦点但未输入搜索词时。在这种情况下,占位符文本具有正确的颜色,但字体似乎恢复为默认值(Helvetica 12 点?)。一旦输入内容或字段失去焦点,就会再次使用正确的字体。
我尝试通过 Apple 文档查找当前未设置的某种字体或颜色设置,但没有成功。我已经摆弄了我可以在界面构建器中找到的所有字体设置,包括可可绑定和检查器中的正常设置。
我需要设置 currentEditor 的一些值吗?我猜不是因为输入文本后字体就会改变..我被卡住了 - 任何人都可以帮忙吗?
编辑:我现在尝试使用 NSTextField,结果是一样的。有没有人有任何想法?