簡體   English   中英

在iPhone中使用未聲明的NSForegroundColorAttributeName錯誤

[英]use of undeclared NSForegroundColorAttributeName error in iphone

在我我想在文本視圖中顯示文本。我也想更改該文本中某些句子的顏色。當我使用下面的代碼時,我得到了未聲明錯誤NSForegroundColorAttributeName,我也嘗試使用kCTForegroundColorAttributeNamevalue但也顯示未聲明的錯誤。如何刪除此錯誤。任何人都可以幫我嗎。

enter code here


NSMutableAttributedString * string = [[NSMutableAttributedString alloc]   initWithString:@"Paint a picture together, alternating every minute (use a timer to keep track)"];
[string addAttribute:kCTForegroundColorAttributeNamevalue:[UIColor redColor] range:NSMakeRange(0,8)];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(8,10)];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(10,5)];

在iOS上,它是PITA ...這是我制作白色並將其應用於范圍的方法

第一

 #import <Foundation/NSAttributedString.h>

然后

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

 CGFloat components[4] = {1.0f, 1.0f, 1.0f, 1.0f};
 CGColorRef whiteColor = CGColorCreate(colorSpace, components);

 CGColorSpaceRelease(colorSpace);

 [mutableAttributedString addAttribute:(NSString*)kCTForegroundColorAttributeName value:(__bridge id)whiteColor range:titleRange];

 CGColorRelease(whiteColor);

我碰巧正在使用ARC,因此如果需要,請取出__bridge。

您需要導入CoreText/CoreText.h獲得的聲明kCTForegroundColorAttributeName

該值必須是CGColor ,而不是UIColor

這樣可以減少強制轉換:

CFAttributedStringSetAttribute((__bridge void*)string, CFRangeMake(0, 8),
    kCTForegroundColorAttributeName, [UIColor redColor].CGColor);

您是否導入了AppKit框架? 另外,在頭文件中包含NSAttributedString.h

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM