样式占位符文本 NSSearchField 和 NSTextField?
Style placeholder text NSSearchField and NSTextField?
我一直在创建一个 MacOS 应用程序,但在设置 NSSearchField(名为 searchField)的字体样式时遇到了问题。到目前为止我的代码如下:
在单个主函数的顶部声明 viewController class:
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
通常这会起作用,但在一种情况下除外:当搜索字段具有焦点但未输入搜索词时。在这种情况下,占位符文本具有正确的颜色,但字体似乎 return 为默认值(Helvetica 12 磅?)。一旦输入内容或字段失去焦点,就会再次使用正确的字体。
我曾尝试通过 Apple 文档查找当前未设置的某种字体或颜色设置,但没有成功。我摆弄了所有可以在界面生成器中找到的字体设置,包括 cocoa 绑定和检查器中的常规设置。
我需要给currentEditor设置一些值吗?我猜不是因为输入文本后字体会更改。我被卡住了 - 谁能帮忙?
编辑:我现在尝试使用 NSTextField,结果是一样的。有人有什么想法吗?
我最终找到了答案。我创建了一个 Formatter
类型的新 class TitleTextFormatter
,即“is intended for subclassing. A custom formatter can restrict the input and enhance the display of data in novel ways”。我需要做的就是覆盖某些默认函数以获得我需要的东西:
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
我一直在创建一个 MacOS 应用程序,但在设置 NSSearchField(名为 searchField)的字体样式时遇到了问题。到目前为止我的代码如下:
在单个主函数的顶部声明 viewController class:
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
通常这会起作用,但在一种情况下除外:当搜索字段具有焦点但未输入搜索词时。在这种情况下,占位符文本具有正确的颜色,但字体似乎 return 为默认值(Helvetica 12 磅?)。一旦输入内容或字段失去焦点,就会再次使用正确的字体。
我曾尝试通过 Apple 文档查找当前未设置的某种字体或颜色设置,但没有成功。我摆弄了所有可以在界面生成器中找到的字体设置,包括 cocoa 绑定和检查器中的常规设置。
我需要给currentEditor设置一些值吗?我猜不是因为输入文本后字体会更改。我被卡住了 - 谁能帮忙?
编辑:我现在尝试使用 NSTextField,结果是一样的。有人有什么想法吗?
我最终找到了答案。我创建了一个 Formatter
类型的新 class TitleTextFormatter
,即“is intended for subclassing. A custom formatter can restrict the input and enhance the display of data in novel ways”。我需要做的就是覆盖某些默认函数以获得我需要的东西:
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