2

When using the below snippet, i get the undefined / undeclared error on

NSForegroundColorAttributeName

This is the url i referred

my code snippet

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

Please let me know

Community
  • 1
  • 1
user198725878
  • 6,266
  • 18
  • 77
  • 135
  • `NSForegroundColorAttributeName` is a part of Mac OSX Core Library. So can u please post some code to let all know how u have implemented. – hp iOS Coder Apr 25 '12 at 06:49
  • @hpiOSCoder: Please help me out, i have included my code. thanks – user198725878 Apr 25 '12 at 08:23
  • Hey yes I'm on it. Actually, NSAttributedString Class reference doc says (in note inside square box) that for iOS u use CoreText framework. So i tried adding the framework, but didnt resolved the error. & at our tough luck the link u provided dont tell anything about it. – hp iOS Coder Apr 25 '12 at 08:30

2 Answers2

8

Try using kCTForegroundColorAttributeName.

Your code

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

Make this change

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

From sample code by apple,

Add following after #import line to your .m file in which u use addAttribute:

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

Pls see if this helps. DONT FORGET TO ADD CORETEXT FRAMEWORK

hp iOS Coder
  • 3,230
  • 2
  • 23
  • 39
  • still i get the same error NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:self.myDisplayTxt]; [string addAttribute:kCTForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,5)]; – user198725878 Apr 25 '12 at 08:34
  • follow this link http://stackoverflow.com/questions/5550739/set-nsattributedstring-to-uitextview – hp iOS Coder Apr 25 '12 at 10:41
  • i get the below error Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteMutableAttributedString getCharacters:range:]: unrecognized selector sent to instance 0x70c5430 – user198725878 Apr 25 '12 at 10:41
  • for which line u r geting the error? Put some breakpoints & debug ur code to see where ur app is terminating – hp iOS Coder Apr 25 '12 at 10:49
  • [self.myTextView setText:(NSString *)string]; in this line it crashes – user198725878 Apr 25 '12 at 11:02
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/10491/discussion-between-user198725878-and-hp-ios-coder) – user198725878 Apr 25 '12 at 11:09
  • ok lets chat.. but before that have u seen link i commented previously? – hp iOS Coder Apr 25 '12 at 11:15
  • Are you using an NSLog to display your NSMutableAttributedString? I get the same error you do when I use NSLog. The error goes away when I comment out the NSLog. – WeekendCodeWarrior Nov 30 '13 at 15:22
0

what version of iOS are you targeting ?

NSForegroundColorAttributeName is a static NSString, declared in the iOS headers, but not part of your application, as is clearly specified in the headers it is available since iOS 6.0.

Pizzaiola Gorgonzola
  • 2,789
  • 1
  • 17
  • 12