2017-02-01 8 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 포인트?). 무언가가 입력되거나 필드의 초점이 사라지 자마자 올바른 글꼴이 다시 사용됩니다.

나는 현재 설정되지 않은 일종의 글꼴이나 색상 설정을 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'입니다. 내가 할 필요가 모든 제가 필요한 것을 얻기위한 특정 기본 기능을 무시했다 : I 추가 viewDidLoad에서 다음

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 

    } 

} 

및 다음

let titleTextFormatter = TitleTextFormatter() 
titleTextField.formatter = titleTextFormatter