[英]NSTextField's cell setPlaceholderAttributedString - placeholder not visible when got focus
我確實使用NSTextFieldCell
對象的setPlaceholderAttributedString:
來設置帶有自定義字體和顏色的占位符。 這是整個代碼...
_statusMessageField = [[NSTextField alloc] initWithFrame:NSZeroRect];
[_statusMessageField setBordered:NO];
[_statusMessageField setBezeled:NO];
[_statusMessageField setFocusRingType:NSFocusRingTypeNone];
[_statusMessageField setDrawsBackground:NO];
[_statusMessageField setTextColor:TM_NS_COLOR(35, 37, 46)];
[_statusMessageField setFont:[NSFont systemFontOfSize:13.0]];
NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc] initWithString:@"What's up?"];
NSRange range = NSMakeRange( 0, [placeholder length] );
[placeholder addAttribute:NSFontAttributeName value:[NSFont systemFontOfSize:13.0] range:range];
[placeholder addAttribute:NSForegroundColorAttributeName value:TM_NS_COLORA( 92, 97, 114, 0.7 ) range:range];
[placeholder fixAttributesInRange:range];
[_statusMessageField.cell setPlaceholderAttributedString:placeholder];
_statusMessageField.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:_statusMessageField];
...這讓我發瘋,因為...
NSTextField
沒有焦點時,占位符才可見, NSTextField
,插入符可見,尚未輸入任何內容,但占位符消失了,這是錯誤的,在至少輸入一個字符之前,它應該是可見的, ...當我將setPlaceholderAttributedString:
替換為setPlaceholderString:
無署名)時,占位符的行為正確-當NSTextField
沒有焦點或它沒有焦點並且尚未輸入任何內容時,該占位符可見。
知道有什么問題嗎?
這是圖像http://d.pr/i/WoC9的鏈接,您可以在其中看到區別。
我認為您需要在NSTextField狀態下選中“拒絕第一響應者”框。
這樣可以解決問題。
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
[_statusMessageField setBordered:NO];
[_statusMessageField setBezeled:NO];
[_statusMessageField setFocusRingType:NSFocusRingTypeNone];
[_statusMessageField setDrawsBackground:NO];
[_statusMessageField setTextColor:[NSColor blueColor]];
[_statusMessageField setFont:[NSFont systemFontOfSize:13.0]];
NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc] initWithString:@"What's up?"];
NSRange range = NSMakeRange( 0, [placeholder length] );
[placeholder addAttribute:NSFontAttributeName value:[NSFont systemFontOfSize:13.0] range:range];
[placeholder addAttribute:NSForegroundColorAttributeName value:[NSColor greenColor] range:range];
[placeholder fixAttributesInRange:range];
[_statusMessageField.cell setPlaceholderAttributedString:placeholder];
_statusMessageField.translatesAutoresizingMaskIntoConstraints = NO;
[_statusMessageField setRefusesFirstResponder:NO];
}
- (void)awakeFromNib {
[_statusMessageField setRefusesFirstResponder:YES];
}
問候,Neelam
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.