Created
April 5, 2013 23:49
-
-
Save mluton/5323538 to your computer and use it in GitHub Desktop.
Put an NSAttributedString into a CATextLayer. This works in iOS 5.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; |
Thank you so much! That helped me, too!)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I had to change
[attributes setObject:[UIColor blackColor] forKey:(NSString*)kCTForegroundColorAttributeName];
into
[attributes setObject:(id)[UIColor blackColor].CGColor forKey:(NSString*)kCTForegroundColorAttributeName];
in order for this to work for me.