【问题标题】:(NSCFType set) - Unrecognized selector in iOS 6(NSCFType set) - iOS 6 中无法识别的选择器
【发布时间】:2012-09-14 01:24:45
【问题描述】:

我正在使用很棒的 TTTAttributedLabel (https://github.com/mattt/TTTAttributedLabel),它在 iOS 5 下运行良好。然而,在 iOS 6 下,我得到了错误:

-[__NSCFType set]: unrecognized selector sent to instance 0x200020e0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-  [__NSCFType set]: unrecognized selector sent to instance 0x200020e0'

稍微研究了这个问题,似乎设置的消息正在发送到已释放的对象。使用调试器,我已经 po'd 0x200020e0 这似乎是一个 CTFontRef。

po 0x200020e0
(int) $0 = 536879328 CTFont <name: .HelveticaNeueUI-Bold, size: 20.000000, matrix: 0x0>
CTFontDescriptor <attributes: <CFBasicHash 0x20001900 [0x3c2a4100]>{type = mutable dict, count = 1,
entries =>
1 : <CFString 0x3be2a768 [0x3c2a4100]>{contents = "NSFontNameAttribute"} = <CFString 0x3c292c14 [0x3c2a4100]>{contents = ".HelveticaNeueUI-Bold"}

}

这让我直接看到了设置 TTTAttributedLabel 的代码:

 [label setText:text afterInheritingLabelAttributesAndConfiguringWithBlock:^ NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
    NSRange boldRange = [[mutableAttributedString string] rangeOfString:title options:NSCaseInsensitiveSearch];
    NSRange strikeRange = [[mutableAttributedString string] rangeOfString:@"sit amet" options:NSCaseInsensitiveSearch];


    UIFont *boldSystemFont = [UIFont boldSystemFontOfSize:20];

    CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL);

    if (font) {
        [mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)font range:boldRange];
        [mutableAttributedString addAttribute:(NSString*)kCTForegroundColorAttributeName value:(__bridge id)font range:boldRange];
        CFRelease(font);
    }

    return mutableAttributedString;
}];

如这里的示例用法:

https://github.com/mattt/TTTAttributedLabel

该代码未经过 ARC 化,因此我添加了桥接强制转换(见上文)。我已经尝试在所有地方保留,但这似乎并没有解决 CTFontRef 过早发布的问题(似乎是)(我认为 - 欢迎其他建议)。

关于如何解决这个问题以及为什么这只会在 iOS 6 模拟器下出现的任何想法?提前致谢。

【问题讨论】:

    标签: ios xcode ios6 core-text ctfontref


    【解决方案1】:

    当我有一个 UILabel myLabel 时,我遇到了同样的错误, 并做 myLabel.attributedText = NSMutableAttributedString。

    试试https://github.com/AliSoftware/OHAttributedLabel

    声明:

    @property (nonatomic, strong) IBOutlet OHAttributedLabel *myLabel;

    而不是 UILabel 解决了错误。

    【讨论】:

      【解决方案2】:

      替换:

      CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL);
      

      与:

      UIFont *font = [UIFont fontWithName:boldSystemFont.fontName size:boldSystemFont.pointSize];
      

      为我修复了 iOS6 中的问题。未在 iOS 5 上测试。

      【讨论】:

        【解决方案3】:

        最后只是一个愚蠢的错误-

        [mutableAttributedString addAttribute:(NSString*)kCTForegroundColorAttributeName value:(__bridge id)font range:boldRange];
        

        应该阅读:

        [mutableAttributedString addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)[UIColor blueColor].CGColor range:boldRange]; 
        

        【讨论】:

          【解决方案4】:

          当我使用错误的范围向 NSMutableAttributedString 添加属性时,我设法得到了这个错误。修复范围修复了崩溃!

          编辑:还设法通过在属性字典中使用 UITextAttributeFont 来获得它,这显然现在在 iOS 7 中已被弃用,这只会让事情神秘地崩溃。替换为 NSForegroundColorAttributeName。

          【讨论】:

          • 抱歉,只是注意到那里的编辑没有多大意义。不记得我指的是哪些键,但您可能能够根据您的情况弄清楚。基本上不要使用已弃用的键!
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-07-13
          • 2014-12-27
          • 2012-03-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多