【问题标题】:NSMutableAttributedString crashing on changing the font?NSMutableAttributedString 在更改字体时崩溃?
【发布时间】:2012-09-13 02:38:48
【问题描述】:

我确定 mutable 意味着它可以更改,那么为什么会发生这种情况?

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;

我的 cattextlayer 和我的 nsmutableattributedsring 都在我的头文件中定义。我在 switch 中对上面的字符串进行了更改,然后调用此代码来更新显示字符串的 cattextlayer:

//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];

尝试设置字体时崩溃,但我不知道为什么?

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

为什么会这样?

【问题讨论】:

  • 这里有一些免费代码演示了属性字符串的使用:github.com/artmayes167/Attribute

标签: objective-c ios nsattributedstring nsmutablestring


【解决方案1】:

首先,你说的attrString是属性吗?如果是属性,你最好检查一下你有没有用copy属性声明这个属性,你大概是在使用编译器生成的setter吗?如果 YES 编译器生成的 setter 将复制消息发送到对象以进行复制。复制消息制作一个不可变的副本。也就是说,它创建的是 NSAttributedString,而不是 NSMutableAttributedString。

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

- (void)setTextCopy:(NSMutableAttributedString *)text {
   textCopy = [text mutableCopy];
}

如果您使用手动引用计数,则像这样:

- (void)setTextCopy:(NSMutableAttributedString *)text {
    [textCopy release];
    textCopy = [text mutableCopy];
}

另一个解决方法是让 textCopy 成为 NSAttributedString 而不是 NSMutableAttributedString,并让其余代码将其作为不可变对象使用。

参考: 1️⃣How to copy a NSMutableAttributedString 2️⃣NSConcreteAttributedString mutableString crash

【讨论】:

    【解决方案2】:

    NSMutableAttributedString 没有 setFont:range: 函数。

    取自这里....iphone/ipad: How exactly use NSAttributedString?

    所以我阅读了一些文档。

    函数是……

    [NSMutableAttirbutedString setAttributes:NSDictionary range:NSRange];
    

    所以你应该能够做这样的事情......

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

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

    如果您仍在使用旧的 ObjC 语法。

    希望对您有所帮助。

    【讨论】:

    • 在使用未声明的 NSFontAttributeName 时出现错误?我还没试过颜色。
    • 我认为这些文档只适用于 os x 10+,也会导致崩溃。
    • 是的,我放了一个字符串而不是 UIFont 代码。我已经在我的 iPhone 上对其进行了测试,使用这条线可以正常工作... [string setAttributes:@{NSBackgroundColorAttributeName:[UIColor greenColor]} range:NSMakeRange(4, 6)];如果您使用它来填充 UILabel,那么您还需要将标签设置为接受属性文本而不仅仅是纯文本。 (在 UIBuilder 中,它是检查器顶部的下拉菜单)
    • 您遇到的错误是什么。我已经测试了代码,所以我知道它可以工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-19
    • 2012-01-07
    相关资源
    最近更新 更多