2012-07-30 62 views
2

我想設置我的數組中的所有範圍的顏色,但我得到這個錯誤。我不明白爲什麼。範圍都是有效的。我甚至嘗試手動插入一個範圍來測試它。謝謝。NSMutableAttributedString - NSForegroundColorAttributeName

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

回答

9

代碼示例與您嘗試構建的代碼示例完全相同嗎?我很肯定NSForegroundColorAttributeName只適用於Mac OS X SDK和iOS 6.0及更高版本,所以示例代碼甚至不應該編譯。

你想要的可能是kCTForegroundColorAttributeName並且通過CGColorRef而不是NSColor

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

但我不知道這是否真的是無效的上下文錯誤的原因。

+0

錯誤:它表示使用未聲明的標識符「kCTForegroundColorAttributeName」...框架? – BDGapps 2012-07-30 20:37:34

+0

明白了,我忘了核心文本。謝謝 – BDGapps 2012-07-30 20:43:48

+0

不錯。所以無效的上下文錯誤消失了?但是當你使用'NSForegroundColorAttributeNameis'時它編譯了嗎?嗯 – 2012-07-30 23:08:59

0

你的情況下是錯誤的,不是你的範圍。你試圖設置一些沒有顏色的東西的顏色。

+0

這是什麼意思在這種情況下。你有解決方案嗎。 – BDGapps 2012-07-30 20:04:32

相關問題