2017-02-01 4 views
1

私はMacOSアプリケーションを作成していて、NSSearchField(searchFieldという名前)のフォントをスタイリングするのに問題があります。次のように私のコードは、これまでのところです:スタイルプレースホルダテキストNSSearchFieldおよびNSTextField?

単一のメインのViewControllerクラスの先頭で宣言:のviewDidLoadで宣言

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) 

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 point?)に戻ります。何かが入力されたり、フィールドがフォーカスを失ったりするとすぐに、正しいフォントがもう一度使用されます。

私はAppleのドキュメントを見て、現在設定されていないフォントや色の設定を調べていないのです。私は、インターフェースビルダーで見つけられるフォント設定すべてで、ココアのバインディングやインスペクタの通常の設定などを手に入れました。

currentEditorの値を設定する必要はありますか?テキストが入力されるとフォントが変更されるので、私は推測していません..私は立ち往生しています - 誰でも助けることができますか?

編集:私は今NSTextFieldで試したところ、結果は同じです。誰にもアイデアはありますか?

+0

あなたが検索バーとして機能するUITextFieldを作ることができますし、あなたがどのようにそれを成形してください。検索バーは、私の理解から適切にカスタマイズするのがはるかに困難です。 – Sethmr

+0

@Sethmr質問を読んだり、タグをよく見てみてください。 –

答えて

0

私は結局答えを見つけることができました。 Formatterという新しいクラス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