有什么办法可以像NSTextField
那样设置NSTextView
的占位符字符串吗?我已经检查过该物业但找不到它。我搜查了一些问题,但没有一个正确的解释。设置NSTextView的占位符字符串
回答
我在网上找到了这个答案。 Philippe Mougin做了这个。
static NSAttributedString *placeHolderString;
@implementation TextViewWithPlaceHolder
+(void)initialize
{
static BOOL initialized = NO;
if (!initialized)
{
NSColor *txtColor = [NSColor grayColor];
NSDictionary *txtDict = [NSDictionary dictionaryWithObjectsAndKeys:txtColor, NSForegroundColorAttributeName, nil];
placeHolderString = [[NSAttributedString alloc] initWithString:@"This is my placeholder text" attributes:txtDict];
}
}
- (BOOL)becomeFirstResponder
{
[self setNeedsDisplay:YES];
return [super becomeFirstResponder];
}
- (void)drawRect:(NSRect)rect
{
[super drawRect:rect];
if ([[self string] isEqualToString:@""] && self != [[self window] firstResponder])
[placeHolderString drawAtPoint:NSMakePoint(0,0)];
}
- (BOOL)resignFirstResponder
{
[self setNeedsDisplay:YES];
return [super resignFirstResponder];
}
@end
雨燕2.0
var placeHolderTitleString: NSAttributedString = NSAttributedString(string: "Place Holder Value", attributes: [NSForegroundColorAttributeName : NSColor.grayColor()])
override func becomeFirstResponder() -> Bool {
self.needsDisplay = true
return super.becomeFirstResponder()
}
override func drawRect(rect: NSRect) {
super.drawRect(rect)
if (self.string! == "") {
placeHolderString.drawAtPoint(NSMakePoint(0, 0))
}
}
斯威夫特4
事实证明,有似乎已经成为一个NSTextView
placeholderAttributedString
属性未公开曝光。因此,您可以简单地在自己的子类中实现它并获取默认的占位符行为(类似于NSTextField
)。
class PlaceholderTextView: NSTextView {
@objc var placeholderAttributedString: NSAttributedString?
}
如果此属性将来可用,则只需使用NSTextView而不是此子类。
我已经提出了一个要求苹果公开此属性的雷达http://www.openradar.me/33583675 – 2017-07-28 04:16:42
这似乎并不奏效。我在一个子类中实现了属性,但不显示它。 – Austin 2017-10-28 17:49:00
@JanApotheker是的,目前还不确定它是在swift4还是在10.13。这不起作用 – 2017-10-29 10:36:41
- 1. 设置NSTextView的占位符字符串,无可可约束
- 2. 我如何设置占位符字符(%)?
- 3. NSComboBox设置为占位符字符串显示
- 4. 设置占位符值
- 5. 使用换行符设置NSTextView字符串
- 6. Scala多行字符串占位符
- 7. 占位符的位置
- 8. 如何使用占位符字符设置JFormattedTextField的值?
- 9. SQL Server中的字符串操作 - 添加占位符字符
- 10. 字符串&字符串的设置值
- 11. 占位符文字
- 12. Arquillian Graphene @位置占位符
- 13. 位置占位符文本
- 14. 使用占位符字符创建字符串
- 15. 在Gtk.ComboBoxText中设置占位符文本?
- 16. 在uisearchbar下为占位符设置UITextField
- 17. 如何设置占位符JS
- 18. 使用xml中的初始字符串时隐藏字符串占位符
- 19. 在字符串中放置可变宽度字体的空间占位符,java
- 20. 设置字符串
- 21. jQuery的帮助:想输入设置占位符值的字段
- 22. 占位符的PDO有效字符
- 23. 设置选择占位符的字体颜色
- 24. 如何在字段中设置占位符文本的样式?
- 25. Swift字符串和可可绑定:不显示占位符字符串
- 26. NSTextView的deleteToBeginningOfLine:删除整个字符串
- 27. PropertyPlaceholderConfigurer位置内的Spring占位符
- 28. 检查插值字符串提供预期的占位符
- 29. .net如何找到占位符的子字符串
- 30. 用动态内容替换字符串中的占位符
这是超级有用,但有关如何使占位符文本换行多行的任何想法? – 2017-03-12 02:44:41