【问题标题】:NSForegroundColorAttributeName undeclaredNSForegroundColorAttributeName 未声明
【发布时间】:2012-04-25 06:09:46
【问题描述】:

当使用下面的 sn-p 时,我得到了 undefined / undeclared error

NSForegroundColorAttributeName

This is the url i referred

我的代码sn-p

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

请告诉我

【问题讨论】:

  • NSForegroundColorAttributeName 是 Mac OSX 核心库的一部分。所以你能不能发布一些代码让大家知道你是如何实现的。
  • @hpiOSCoder:请帮帮我,我已经包含了我的代码。谢谢
  • 嘿,是的,我在上面。实际上,NSAttributedString 类参考文档说(在方框内的注释中)对于 iOS,你使用 CoreText 框架。所以我尝试添加框架,但没有解决错误。并且运气不好,您提供的链接没有说明任何内容。

标签: ios ios4


【解决方案1】:

尝试使用 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)];

来自苹果的示例代码,

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

#if (TARGET_OS_EMBEDDED || TARGET_OS_IPHONE)
#import <CoreText/CoreText.h>
#else
#import <AppKit/AppKit.h>
#endif

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

【讨论】:

  • 我仍然得到同样的错误 NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:self.myDisplayTxt]; [string addAttribute:kCTForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,5)];
  • 我收到以下错误 Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteMutableAttributedString getCharacters:range:]: unrecognized selector sent to instance 0x70c5430
  • 您在哪一行得到错误?放置一些断点并调试您的代码以查看您的应用程序终止的位置
  • [self.myTextView setText:(NSString *)string];在这一行它崩溃了
【解决方案2】:

您的目标是什么版本的 iOS?

NSForegroundColorAttributeName 是一个静态 NSString,在 iOS 标头中声明,但不是应用程序的一部分,正如标头中明确指出的那样,它自 iOS 6.0 起可用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-20
    • 1970-01-01
    • 1970-01-01
    • 2015-01-01
    • 2010-12-19
    • 2011-02-06
    • 2021-11-21
    • 2013-05-07
    相关资源
    最近更新 更多