有什么办法可以像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. ExtJS的 - 占位符的字符串
- 5. 设置占位符值
- 6. Java的占位符比较字符串
- 7. 使用换行符设置NSTextView字符串
- 8. asp:占位符内容到字符串
- 9. Scala多行字符串占位符
- 10. 格式化字符串占位符
- 11. 占位符的位置
- 12. 如何使用占位符字符设置JFormattedTextField的值?
- 13. SQL Server中的字符串操作 - 添加占位符字符
- 14. 设置Internet Explorer的占位符?
- 15. 字符串&字符串的设置值
- 16. 如何在使用jQuery时为占位符设置onblur和onfocus字符串
- 17. 占位符文字
- 18. Arquillian Graphene @位置占位符
- 19. 位置占位符文本
- 20. 如何在字符串中使用占位符作为字符?
- 21. 使用占位符字符创建字符串
- 22. 包含%符号的字符串中的Python占位符
- 23. 占位符通配符htaccess的字符串的RewriteCond
- 24. 在Gtk.ComboBoxText中设置占位符文本?
- 25. 在IE9中设置占位符att
- 26. 如何设置占位符JS
- 27. 在uisearchbar下为占位符设置UITextField
- 28. html:text如何设置占位符属性
- 29. 使用xml中的初始字符串时隐藏字符串占位符
- 30. Sitecore - WFFM占位符设置无法从文件夹添加占位符
这是超级有用,但有关如何使占位符文本换行多行的任何想法? – 2017-03-12 02:44:41