样式占位符文本NSSearchField和NSTextField?

时间:2017-02-01 17:00:01

标签: swift macos fonts nssearchfield

我一直在创建一个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,结果是一样的。有没有人有任何想法?

1 个答案:

答案 0 :(得分:1)

我最终找到了答案。我创建了一个类型为TitleTextFormatter的新类Formatter,即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