Create CATextLayer in iOS
| |It's a bit tricky th create a CATextLayer in iOS:
Firstly, remember to explicitly set the frame size of the layer:
CATextLayer *textLayer = [CATextLayer layer];
// set the string
textLayer.string = @"Hello world";
textLayer.foregroundColor = [UIColor blackColor].CGColor;
textLayer.frame = CGRectMake(0, 0, 100, 20);
Then you may wonder how to set the CFTypeRef
type for font
. Actually simply cast the font name will do:
textLayer.font = (__bridge CFTypeRef)@"AmericanTypewriter-CondensedLight";
textLayer.fontSize = 18;
textLayer.alignmentMode = kCAAlignmentCenter;
Lastly, remember to set the contentsScale to match the scale of the screen of the device, otherwise the output will be very ugly on retina display device:
textLayer.contentsScale = [[UIScreen mainScreen] scale];