gpt4 book ai didi

objective-c - 为没有 Cocoa 绑定(bind)的 NSTextView 设置占位符字符串

转载 作者:行者123 更新时间:2023-12-03 17:11:14 25 4
gpt4 key购买 nike

我知道可以在 Cocoa 绑定(bind)中设置 NSTextFieldNSTextView 的“空占位符”。此外,对于 NSTextField,可以在 Xcode 界面生成器中设置占位符,或者使用 aTextField.placeholderString = ... 设置。如果我不使用 Cocoa 绑定(bind),如何设置 NSTextView 的占位符字符串?

最佳答案

NSTextView 中的 placeholderAttributedString 属性未公开公开。因此,您可以简单地在自己的子类中实现它并获得默认的占位符行为:

1 - 创建 NSTextView 的子类

class CustomTextView: NSTextView {
var placeholderAttributedString: NSAttributedString?
}

2 - 在 View Controller 中使用

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

关于objective-c - 为没有 Cocoa 绑定(bind)的 NSTextView 设置占位符字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26267488/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com