2014-10-08 68 views
0

我知道NSTextFieldNSTextView的「空佔位符」可以在Cocoa綁定中設置。另外,對於NSTextField,可以在Xcode界面構建器中設置佔位符,或使用aTextField.placeholderString = ...。如果我不使用Cocoa綁定,我如何設置NSTextView的佔位符字符串?設置NSTextView的佔位符字符串,無可可約束

+0

此信息可能幫助,如果你還沒有看過那裏http://stackoverflow.com/questions/24931311/xcode-style-placeholders-in-an-nstextview – 2014-10-08 22:48:54

+0

這是有益的,謝謝!我認爲會有更簡單的方式,就像文本字段一樣。 – Ethan 2014-10-08 23:34:57

+0

另請參閱:http://stackoverflow.com/questions/29428594/set-the-placeholder-string-for-nstextview – JWWalker 2015-05-27 16:32:36

回答

1

NSTextView中的placeholderAttributedString屬性未公開公開。因此,你可以簡單地實現它自己的子類,並得到默認的佔位行爲:

1 - NSTextView

的創建子類
class CustomTextView: NSTextView { 
      var placeholderAttributedString: NSAttributedString? 
     } 

2 - 使用您的ViewController

let attributes : [String : Any] = [NSFontAttributeName : NSFont(name: "HelveticaNeue-Light", size: 19)!, NSForegroundColorAttributeName : NSColor.green] 
    yourTextView.placeholderAttributedString = NSAttributedString(string: "Enter your Name", attributes: attributes)