【问题标题】:NSMutableAttributedString - NSForegroundColorAttributeNameNSMutableAttributedString - NSForegroundColorAttributeName
【发布时间】:2012-07-30 19:33:12
【问题描述】:

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

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 = @"";

【问题讨论】:

    标签: iphone objective-c ios ios5 ios4


    【解决方案1】:

    代码示例与您尝试构建的代码完全相同吗?我很确定NSForegroundColorAttributeName 仅在 Mac OS X SDK 和 iOS 6.0 及更高版本中可用,因此示例代码甚至不应该编译。

    您想要的可能是 kCTForegroundColorAttributeName 并传递 CGColorRef 而不是 NSColor

    [string addAttribute:(id)kCTForegroundColorAttributeName
                   value:(id)[UIColor redColor].CGColor
                   range:NSRangeFromString(s)];
    

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

    【讨论】:

    • 错误:它说使用了未声明的标识符“kCTForegroundColorAttributeName”...框架???
    • 知道了,我忘记了核心文本。谢谢
    • 不错。所以无效的上下文错误消失了?但是当你使用NSForegroundColorAttributeNameis 时它编译了吗?嗯
    • 仅供参考,NSForegroundColorAttributeName 自 iOS 6.0 起可用,因此不会让人感到困惑。
    【解决方案2】:

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

    【讨论】:

    • 在这种情况下是什么意思。你有解决办法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多