Objective c NSMutableAttributedString在更改字体时崩溃?

Objective c NSMutableAttributedString在更改字体时崩溃?,objective-c,ios,nsattributedstring,nsmutablestring,Objective C,Ios,Nsattributedstring,Nsmutablestring,我相信可变意味着它可以改变,那么为什么会发生这种情况呢 attrString = [[NSMutableAttributedString alloc] initWithString:@"Tip 1: Aisle Management The most obvious step – although one that still has not been taken by a disconcerting number of organisations – is to configure cabin

我相信可变意味着它可以改变,那么为什么会发生这种情况呢

attrString = [[NSMutableAttributedString alloc] initWithString:@"Tip 1: Aisle Management The most obvious step – although one that still has not been taken by a disconcerting number of organisations – is to configure cabinets in hot and cold aisles. If you haven’t got your racks into cold and hot aisle configurations, we can advise ways in which you can achieve improved airflow performance."];

        [attrString setFont:[UIFont systemFontOfSize:20] range:NSMakeRange(0, 23)];
        [attrString setFont:[UIFont systemFontOfSize:15] range:NSMakeRange(24, 325)];
        [attrString setTextColor:[UIColor blackColor] range:NSMakeRange(0,184)];
        [attrString setTextColor:[UIColor blueColor] range:NSMakeRange(185,325)];
        break;
catextlayer和nsmutableattributedsring都在头文件中定义。我在一个开关中对上面的字符串进行了更改,然后调用此代码来更新catextlayer,该字符串如图所示:

//updates catext layer
TextLayer = [CATextLayer layer];

TextLayer.bounds = CGRectMake(0.0f, 0.0f, 245.0f, 290.0f);
TextLayer.string = attrString;
TextLayer.position = CGPointMake(162.0, 250.0f);
TextLayer.wrapped = YES;

[self.view.layer addSublayer:TextLayer];
当它试图设置字体时会崩溃,但我不知道为什么

-[NSContectEmustableAttributedString setFont:range::发送到实例0xd384420的无法识别的选择器 *由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[nsConcreteModableAttributedString setFont:range:::发送到实例0xd384420的选择器无法识别”


为什么会发生这种情况?

NSMutableAttributedString没有setFont:range:函数

attrString = [[NSMutableAttributedString alloc] initWithString:@"Tip 1: Aisle Management The most obvious step – although one that still has not been taken by a disconcerting number of organisations – is to configure cabinets in hot and cold aisles. If you haven’t got your racks into cold and hot aisle configurations, we can advise ways in which you can achieve improved airflow performance."];

        [attrString setFont:[UIFont systemFontOfSize:20] range:NSMakeRange(0, 23)];
        [attrString setFont:[UIFont systemFontOfSize:15] range:NSMakeRange(24, 325)];
        [attrString setTextColor:[UIColor blackColor] range:NSMakeRange(0,184)];
        [attrString setTextColor:[UIColor blueColor] range:NSMakeRange(185,325)];
        break;
从这里开始

所以我读了一些文档

功能是

[NSMutableAttirbutedString setAttributes:NSDictionary range:NSRange];
所以你应该可以这样做

[string setAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Helvetice-Neue"]} range:NSMakeRange(0, 2)];

如果您仍然使用旧的ObjC语法


希望对您有所帮助。

首先,您所说的attrString是属性吗?如果它是属性,您最好检查您是否已使用copy属性声明了属性,并且您可能正在使用编译器生成的setter?如果是,编译器生成的setter将向对象发送复制消息以进行复制。复制消息生成不可变的副本。也就是说,它创建的是NSAttributedString,而不是NSMutableAttributedString

解决此问题的一种方法是编写自己的setter,该setter使用mutableCopy,如果您使用的是ARC:

- (void)setTextCopy:(NSMutableAttributedString *)text {
   textCopy = [text mutableCopy];
}
或者,如果您使用手动引用计数:

- (void)setTextCopy:(NSMutableAttributedString *)text {
    [textCopy release];
    textCopy = [text mutableCopy];
}
另一个修复方法是将textCopy设置为NSAttributedString而不是NSMutableAttributedString,并将其余代码作为不可变对象使用

参考: 1.️⃣
2.️⃣

我在使用未声明的NSFontAttributeName时出错?我还没有试过这些颜色。看了文档后编辑。我认为这些文档只适用于OSX10+,这也会导致崩溃。是的,我用字符串代替UIFont代码。我已经在我的iPhone上测试过了,使用这条线它工作得很好。。。[string setAttributes:@{NSBackgroundColorAttributeName:[UIColor greenColor]}范围:NSMakeRange(4,6)];如果使用它来填充UILabel,则还需要将标签设置为接受属性文本而不仅仅是纯文本。(在UIBuilder中,它是检查器顶部的下拉列表)您得到的错误是什么。我已经测试了代码,所以我知道它是有效的