首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSMutableAttributedString - NSForegroundColorAttributeName

NSMutableAttributedString - NSForegroundColorAttributeName
EN

Stack Overflow用户
提问于 2012-07-31 03:33:13
回答 2查看 5.7K关注 0票数 2

我正在尝试设置数组中所有范围的颜色,但是我得到了这个错误。我不明白为什么。范围都是有效的。我甚至尝试手动插入一个范围来测试它。谢谢。

CGContextSetFillColorWithColor:上下文0x0无效

代码语言:javascript
复制
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 = @"";
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-07-31 04:29:07

代码示例与您尝试构建的代码是否完全相同?我非常确定NSForegroundColorAttributeName只在Mac和iOS 6.0及更高版本中可用,所以示例代码甚至不应该编译。

相反,您需要的可能是kCTForegroundColorAttributeName,并传递一个CGColorRef而不是NSColor

代码语言:javascript
复制
[string addAttribute:(id)kCTForegroundColorAttributeName
               value:(id)[UIColor redColor].CGColor
               range:NSRangeFromString(s)];

但我不确定这是否真的是无效上下文错误的原因。

票数 9
EN

Stack Overflow用户

发布于 2012-07-31 03:48:12

你的上下文是错误的,而不是你的范围。您正在尝试设置没有颜色的对象的颜色。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11728307

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档