In my i want to i'm displaying a text in text view.Also i want to change the color of some sentences in that text.When i'm using the code below i'm getting the error NSForegroundColorAttributeName undeclared and i also tried using kCTForegroundColorAttributeNamevalue but is also showing undeclared error.How to remove this error.Can any one help me please.
enter code here
NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:@"Paint a picture together, alternating every minute (use a timer to keep track)"];
[string addAttribute:kCTForegroundColorAttributeNamevalue:[UIColor redColor] range:NSMakeRange(0,8)];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(8,10)];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(10,5)];
On iOS its a PITA... Here is how I am making a white color and applying it to a range
First
#import <Foundation/NSAttributedString.h>
Then
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGFloat components[4] = {1.0f, 1.0f, 1.0f, 1.0f};
CGColorRef whiteColor = CGColorCreate(colorSpace, components);
CGColorSpaceRelease(colorSpace);
[mutableAttributedString addAttribute:(NSString*)kCTForegroundColorAttributeName value:(__bridge id)whiteColor range:titleRange];
CGColorRelease(whiteColor);
I happen to be using ARC, so take out the __bridge if you have to.
You need to import CoreText/CoreText.h
to get the declaration of kCTForegroundColorAttributeName
.
The value must be a CGColor
, not a UIColor
.
There's less casting to do this way:
CFAttributedStringSetAttribute((__bridge void*)string, CFRangeMake(0, 8),
kCTForegroundColorAttributeName, [UIColor redColor].CGColor);
Have you imported the AppKit framework? Also, include NSAttributedString.h
in your header file.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.