ios - NSForegroundColorAttributeName 未声明

标签 ios ios4

当使用下面的代码片段时,我在

上得到了 undefined/undeclared error

NSForegroundColorAttributeName

This is the url i referred

我的代码片段

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

请告诉我

最佳答案

尝试使用 kCTForegroundColorAttributeName。

你的代码

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

进行此更改

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

来自 apple 的示例代码,

在您使用 addAttribute:

的 .m 文件中的 #import 行之后添加以下内容
#if (TARGET_OS_EMBEDDED || TARGET_OS_IPHONE)
#import <CoreText/CoreText.h>
#else
#import <AppKit/AppKit.h>
#endif

请看看这是否有帮助。不要忘记添加核心文本框架

关于ios - NSForegroundColorAttributeName 未声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10310223/

相关文章:

iphone - 尝试在设备上运行测试时出现奇怪的错误

ios - 字符串有什么区别?和字符串! (创建可选变量的两种方法)?

ios - 无法导入 pod Google/Analytics 的框架目标

ios - 从 xib 文件中以模态方式呈现 View Controller

iphone - IOS 4.3 UINavigationBar tintColor 泄漏

ios - 将文本从文本文件传递到 "Swift 3"中的 UITextView

iphone - 我可以在 iAds 被点击后隐藏它们吗?

iphone - 对象类型更改

iphone - 使用对象子类的属性

objective-c - 以编程方式绘制粗略的线条