1

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

4

1 回答 1

2

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

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
于 2018-01-28T10:24:03.600 回答