我正在尝试设置数组中所有范围的颜色,但是我得到了这个错误。我不明白为什么。范围都是有效的。我甚至尝试手动插入一个范围来测试它。谢谢。
CGContextSetFillColorWithColor:上下文0x0无效
NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:tv.text];
for (NSString * s in array) {
[string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSRangeFromString(s)];
}
CATextLayer *textlayer = [[CATextLayer alloc]init];
textlayer.frame = CGRectMake(0, 0, 320, 480);
[self.view.layer addSublayer:textlayer];
textlayer.string = @"aString"; //works
textlayer.string = string; //does not work
tv.text = @"";发布于 2012-07-31 04:29:07
代码示例与您尝试构建的代码是否完全相同?我非常确定NSForegroundColorAttributeName只在Mac和iOS 6.0及更高版本中可用,所以示例代码甚至不应该编译。
相反,您需要的可能是kCTForegroundColorAttributeName,并传递一个CGColorRef而不是NSColor。
[string addAttribute:(id)kCTForegroundColorAttributeName
value:(id)[UIColor redColor].CGColor
range:NSRangeFromString(s)];但我不确定这是否真的是无效上下文错误的原因。
发布于 2012-07-31 03:48:12
你的上下文是错误的,而不是你的范围。您正在尝试设置没有颜色的对象的颜色。
https://stackoverflow.com/questions/11728307
复制相似问题