Skip to content

Instantly share code, notes, and snippets.

@ytlvy
Forked from mluton/gist:5323538
Created September 21, 2016 04:06
Show Gist options
  • Save ytlvy/bf52070533e23435d9a9c2744f7a194c to your computer and use it in GitHub Desktop.
Save ytlvy/bf52070533e23435d9a9c2744f7a194c to your computer and use it in GitHub Desktop.
Put an NSAttributedString into a CATextLayer. This works in iOS 5.
CATextLayer *textLayer = [CATextLayer layer];
textLayer.backgroundColor = [UIColor whiteColor].CGColor;
textLayer.frame = CGRectMake(20, 20, 200, 100);
textLayer.contentsScale = [[UIScreen mainScreen] scale];
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 blackColor] forKey:(NSString*)kCTForegroundColorAttributeName];
NSAttributedString *attrStr = [[NSAttributedString alloc] initWithString:@"Lorem Ipsum" attributes:attributes];
textLayer.string = attrStr;
[self.view.layer addSublayer:textLayer];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment