rwb*_*ler 13 xcode core-text ios ctfontref ios6
我正在使用伟大的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'
Run Code Online (Sandbox Code Playgroud)
稍微研究了这个问题后,似乎将设置的消息发送到已经发布的对象.使用调试器,我已经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"}
Run Code Online (Sandbox Code Playgroud)
}
这让我直接找到了设置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;
}];
Run Code Online (Sandbox Code Playgroud)
如在此处的示例用法:
https://github.com/mattt/TTTAttributedLabel
该代码不是ARCified所以我添加了桥接转换(见上文).我已经尝试过保留所有地方,但这似乎并没有解决CTFontRef过早发布的问题(似乎是)(我认为 - 欢迎其他建议).
关于如何解决这个问题的任何想法以及为什么这只会在iOS 6模拟器下出现?提前致谢.
取代:
CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL);
Run Code Online (Sandbox Code Playgroud)
有:
UIFont *font = [UIFont fontWithName:boldSystemFont.fontName size:boldSystemFont.pointSize];
Run Code Online (Sandbox Code Playgroud)
修复了iOS6中的问题.未在iOS 5上测试过.
最终这只是一个愚蠢的错误——
[mutableAttributedString addAttribute:(NSString*)kCTForegroundColorAttributeName value:(__bridge id)font range:boldRange];
Run Code Online (Sandbox Code Playgroud)
应该读过:
[mutableAttributedString addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)[UIColor blueColor].CGColor range:boldRange];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11389 次 |
| 最近记录: |