我正在尝试将 Attributed 字符串放入 NSTextField 中,该字符串本身位于 NSAlert 中 这是我的代码:
NSTextField *label1 = [[NSTextField alloc]initWithFrame:NSMakeRect(0, 23, 50, 20)];
[label1 setEditable:FALSE];
[label1 setAllowsEditingTextAttributes:TRUE];
[label1 setBezeled:FALSE];
label1.backgroundColor = [NSColor clearColor];
NSString *login = @"Username";
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:login];
NSString *boldFontName = [[NSFont boldSystemFontOfSize:12] fontName];
[attrString beginEditing];
NSRange ran = NSMakeRange(0, 8);
[attrString addAttribute:NSFontAttributeName
value:boldFontName
range:ran];
[attrString endEditing];
NSLog(@"%@",attrString);
[label1 setAttributedStringValue:attrString];
[alert setAccessoryView:label1];
[alert runModal];
但是,一旦调用 [alert runModal],我的应用程序就会崩溃
"[__NSCFConstantString pointSize]: unrecognized selector sent to instance 0x7fff74035bb0"
我不确定为什么会这样。看来它与字符串有关,因为只要我删除 [alert setAccessoryView:label1] 或给 label1 一个标准的 nsstring,它就可以正常工作。请帮忙!