NSForegroundColorAttributeName未声明

use*_*878 2 ios4 ios

当使用下面的片段时,我得到了undefined / undeclared error开启

NSForegroundColorAttributeName

这是我提到的网址

我的代码片段

NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:self.myDisplayTxt];
    [string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,5)];
Run Code Online (Sandbox Code Playgroud)

请告诉我

hp *_*der 8

尝试使用kCTForegroundColorAttributeName.

你的代码

NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:self.myDisplayTxt];
    [string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,5)];
Run Code Online (Sandbox Code Playgroud)

进行此更改

[string addAttribute:(NSString*)kCTForegroundColorAttributeName 
                value:(id)[[UIColor redColor] CGColor]
                range:NSMakeRange(0,5)];
Run Code Online (Sandbox Code Playgroud)

来自apple的示例代码,

在#import行之后添加以下内容到您使用的.m文件中 addAttribute:

#if (TARGET_OS_EMBEDDED || TARGET_OS_IPHONE)
#import <CoreText/CoreText.h>
#else
#import <AppKit/AppKit.h>
#endif
Run Code Online (Sandbox Code Playgroud)

请看看这是否有帮助.不要忘记加入CORETEXT框架