I have a simple example application where I create a CATextLayer and set its string property to NSAttributedString . Then I add that CATextLayer to the view.
#import <CoreText/CoreText.h> #import <QuartzCore/QuartzCore.h> @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; CTFontRef fontFace = CTFontCreateWithName((__bridge CFStringRef)(@"HelfaSemiBold"), 24.0, NULL); NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init]; [attributes setObject:(__bridge id)fontFace forKey:(NSString*)kCTFontAttributeName]; [attributes setObject:[UIColor blueColor] forKey:(NSString*)kCTForegroundColorAttributeName]; NSAttributedString *attrStr = [[NSAttributedString alloc] initWithString:@"Lorem Ipsum" attributes:attributes]; CATextLayer *textLayer = [CATextLayer layer]; textLayer.backgroundColor = [UIColor whiteColor].CGColor; textLayer.frame = CGRectMake(20, 20, 200, 100); textLayer.contentsScale = [[UIScreen mainScreen] scale]; textLayer.string = attrStr; [self.view.layer addSublayer:textLayer]; }
This works great in a simulator, except that the text is black, not blue. When working on the device, everything is displayed on the simulator, but it generates the following two errors in the console.
<Error>: CGContextSetFillColorWithColor: invalid context 0x0 <Error>: CGContextSetStrokeColorWithColor: invalid context 0x0
Should I install CGContext somewhere? Am I setting my attributes incorrectly? I am completely mistaken. Please note: this is for iOS 5.x application, and I want to use CATextLayer for performance reasons. A real application will have many CATextLayer s.
ios objective-c
Michael luton
source share