gpt4 book ai didi

objective-c - [__NSCFConstantString pointSize] : unrecognized selector sent to instanc

转载 作者:太空狗 更新时间:2023-10-30 03:29:57 25 4
gpt4 key购买 nike

我正在尝试将属性字符串放入 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,它就可以正常工作。请帮忙!

最佳答案

你做对了。但是你犯了一个小错误。您已将 NSString 作为 NSFontAttributeName 的属性传递,但它需要 NSFont

试试这个。

    NSFont *boldFontName = [NSFont boldSystemFontOfSize:12];
[attrString beginEditing];
NSRange ran = NSMakeRange(0, 8);
[attrString addAttribute:NSFontAttributeName
value:boldFontName
range:ran];

关于objective-c - [__NSCFConstantString pointSize] : unrecognized selector sent to instanc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14001477/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com