Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add text to CALayer

Tags:

iphone

Is it possible to add a UILabel to a CALayer without subclassing and drawing it in drawInContext:?

Thanks!

like image 990
christo16 Avatar asked Feb 05 '10 19:02

christo16


People also ask

How do I add text in CALayer?

Your UILabel already has a CALayer behind it. If you are putting together several CALayers, you can just add the UILabel's layer as a sublayer of one of those (by using its layer property). If it's direct text drawing in a layer that you want, the UIKit NSString additions that Deepak points to are the way to go.

What is layer Swift?

Overview. Layers are often used to provide the backing store for views but can also be used without a view to display content. A layer's main job is to manage the visual content that you provide but the layer itself has visual attributes that can be set, such as a background color, border, and shadow.


1 Answers

CATextLayer *label = [[CATextLayer alloc] init]; [label setFont:@"Helvetica-Bold"]; [label setFontSize:20];   [label setFrame:validFrame]; [label setString:@"Hello"]; [label setAlignmentMode:kCAAlignmentCenter]; [label setForegroundColor:[[UIColor whiteColor] CGColor]]; [layer addSublayer:label];  [label release]; 
like image 132
Mshah2 Avatar answered Nov 11 '22 11:11

Mshah2