这是发生错误的行。
[self addAttribute:(__bridge NSString*)kCTForegroundColorAttributeName value:(__bridge id)color.CGColor range:range];
这是完整的方法代码。
-(void)setTextColor:(UIColor*)color range:(NSRange)range
{
if (range.location != NSNotFound) {
// kCTForegroundColorAttributeName
[self removeAttribute:(__bridge NSString*)kCTForegroundColorAttributeName range:range]; // Work around for Apple leak
[self addAttribute:(__bridge NSString*)kCTForegroundColorAttributeName value:(__bridge id)color.CGColor range:range];
}}
我尝试测试是否找不到范围,但错误仍然存在。关于我做错了什么的提示或建议?
答案 0 :(得分:0)
这是因为(__bridge id)color.CGColor
是nil
就这样做,没有任何额外的转换
[self addAttribute:NSForegroundColorAttributeName value:color range:range];
答案 1 :(得分:0)
错误地检查你的情况,你需要:
if (range.location >= 0 && range.location < self.length){
if (range.location + range.length >= self.length)
range.length = self.length - range.location;
[self removeAttribute:(__bridge NSString*)kCTForegroundColorAttributeName range:range]; // Work around for Apple leak
[self addAttribute:(__bridge NSString*)kCTForegroundColorAttributeName value:(__bridge id)color.CGColor range:range];
}