样式占位符文本NSSearchField和NSTextField



我一直在创建一个MacOS应用程序,在设置NSSearchField(名为searchField)的字体样式时遇到问题。到目前为止,我的代码如下:

在单个主视图的顶部声明控制器类:

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点?)。一旦输入了内容或字段失去焦点,就会再次使用正确的字体。

我在苹果文档中查找了一些目前尚未设置的字体或颜色设置,但没有成功。我在界面构建器中摆弄了所有可以找到的字体设置,包括可可绑定和检查器中的正常设置。

我需要设置currentEditor的一些值吗?我猜不是因为输入文本后字体就变了。。我被卡住了,有人能帮忙吗?

编辑:我现在尝试了NSTextField,结果是一样的。有人有什么想法吗?

我最终找到了答案。我创建了一个类型为Formatter的新类TitleTextFormatter,它是用于子类化的。自定义格式化程序可以以新颖的方式限制输入并增强数据显示。我所需要做的就是覆盖某些默认功能来获得我需要的东西:

import Cocoa
class TitleTextFormatter: Formatter {
override func string(for obj: Any?) -> String? {    
/*
* this function receives the object it is attached to.
* in my case it only ever receives an NSConcreteAttributedString
* and returns a plain string to be formatted by other functions
*/
var result: String? = nil
if let attrStr = obj as? NSAttributedString {
result = attrStr.string
}
return result
}
override func getObjectValue( _ obj: AutoreleasingUnsafeMutablePointer<AnyObject?>?,
for string: String,
errorDescription error: AutoreleasingUnsafeMutablePointer<NSString?>?) -> Bool {
/* 
* this function in general is overridden to provide an object created 
* from the input string. in this instance, all I want is an attributed string
*/
let titleParagraphStyle = NSMutableParagraphStyle()
titleParagraphStyle.alignment = .center
let titleAttributes = [NSAttributedStringKey.foregroundColor: NSColor.mainText,
NSAttributedStringKey.font: NSFont.titleText,
NSAttributedStringKey.paragraphStyle: titleParagraphStyle]
let titleAttrStr = NSMutableAttributedString(string: string,
attributes: titleAttributes)
obj?.pointee = titleAttrStr
return true
}
override func attributedString(for obj: Any,
withDefaultAttributes attrs: [NSAttributedStringKey : Any]? = nil) -> NSAttributedString? {
/* 
* is overridden to show that an attributed string is created from the
* formatted object. this happens to duplicate what the previous function 
* does, only because the object I want to create from string is an 
* attributed string
*/
var titleAttrStr: NSMutableAttributedString?
if let str = string(for: obj) {
let titleParagraphStyle = NSMutableParagraphStyle()
titleParagraphStyle.alignment = .center
let titleAttributes = [NSAttributedStringKey.foregroundColor: NSColor.mainText,
NSAttributedStringKey.font: NSFont.titleText,
NSAttributedStringKey.paragraphStyle: titleParagraphStyle]
titleAttrStr = NSMutableAttributedString(string: str,
attributes: titleAttributes)
}
return titleAttrStr
}
}   

然后在viewDidLoad中,我添加了以下内容:

let titleTextFormatter = TitleTextFormatter()
titleTextField.formatter = titleTextFormatter

相关内容

  • 没有找到相关文章

最新更新