12

iOS 5 で正常に動作する優れた TTTAttributedLabel ( https://github.com/mattt/TTTAttributedLabel ) を使用しています。ただし、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'

この問題を少し調べたところ、設定されたメッセージが解放されたオブジェクトに送信されているようです。デバッガーを使用して、CTFontRef のように見える 0x200020e0 を po しました。

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 シミュレーターでのみ発生する理由についてのアイデアはありますか? 前もって感謝します。

4

4 に答える 4

6

置換:

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

と:

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

私のためにiOS6の問題を修正しました。iOS 5 ではテストされていません。

于 2013-03-28T11:03:46.087 に答える
4

結局、それはただの愚かな間違いだった -

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

読むべきだった:

[mutableAttributedString addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)[UIColor blueColor].CGColor range:boldRange]; 
于 2013-04-11T15:13:57.103 に答える
2

UILabel myLabel を持っていて、myLabel.attributedText = NSMutableAttributedString を実行すると、同じエラーが発生しました。

https://github.com/AliSoftware/OHAttributedLabel を試してください

宣言:

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

UILabel の代わりにエラーを解決します。

于 2012-10-31T08:29:44.143 に答える
1

I managed to get this error when I was using a bad range adding attributes to an NSMutableAttributedString. Fixing the range fixed the crash!

EDIT: Also managed to get it by using UITextAttributeFont in the attributes dictionary which is apparently now deprecated in iOS 7 which just makes things crash mysteriously. The replacement is NSForegroundColorAttributeName.

于 2014-04-25T12:07:52.743 に答える