【发布时间】: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