[英]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.