2014-10-08 70 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)