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