ben*_*555 8 macos cocoa objective-c nsattributedstring nsalert
我试图将一个归因字符串放在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];
Run Code Online (Sandbox Code Playgroud)
但是,一旦调出[alert runModal],我的应用程序就会崩溃
"[__NSCFConstantString pointSize]: unrecognized selector sent to instance 0x7fff74035bb0"
Run Code Online (Sandbox Code Playgroud)
我不确定为什么会这样.它似乎与字符串有关,因为只要我删除[alert setAccessoryView:label1]或给label1一个标准的nsstring,它就可以正常工作.请帮忙!
Ram*_*j T 38
你做得对.但你犯了一个小错误.您已将其NSString作为属性传递NSFontAttributeName但预期NSFont.
试试这个.
NSFont *boldFontName = [NSFont boldSystemFontOfSize:12];
[attrString beginEditing];
NSRange ran = NSMakeRange(0, 8);
[attrString addAttribute:NSFontAttributeName
value:boldFontName
range:ran];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6526 次 |
| 最近记录: |