2

以下のスニペットを使用するとundefined / undeclared error

NSForegroundColorAttributeName

これは私が参照したURLです

私のコードスニペット

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

私にお知らせください

4

2 に答える 2

8

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)];

アップルのサンプルコードから、

uが使用する.mファイルに#import行の後に次を追加しますaddAttribute:

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

plsはこれが役立つかどうかを確認します。コアテキストフレームワークを追加することを忘れないでください

于 2012-04-25T08:32:08.790 に答える
0

どのバージョンのiOSをターゲットにしていますか?

NSForegroundColorAttributeNameは静的NSStringであり、iOSヘッダーで宣言されていますが、iOS 6.0以降で使用可能なヘッダーで明確に指定されているように、アプリケーションの一部ではありません。

于 2013-09-13T08:20:55.773 に答える