Graphics Reference
In-Depth Information
What we really want is a UILabel subclass that actually uses a CATextLayer as its
backing layer , then it would automatically resize with the view and there would be no
redundant backing image to worry about.
As we discussed in Chapter 1, “The Layer Tree,” every UIView is backed by an instance
of CALayer . That layer is automatically created and managed by the view, so how can we
substitute a different layer type? We can't replace the layer once it has been created, but if
we subclass UIView , we can override the + layerClass method to return a different
layer subclass at creation time. UIView calls the + layerClass method during its
initialization, and uses the class it returns to create its backing layer.
Listing 6.4 shows the code for a UILabel subclass called LayerLabel that draws its
text using a CATextLayer instead of the using the slower -drawRect: approach that
an ordinary UILabel uses. LayerLabel instances can either be created program-
matically or via Interface Builder by adding an ordinary label to the view and setting its
class to LayerLabel .
Listing 6.4 LayerLabel , a UILabel Subclass That Uses CATextLayer
#import "LayerLabel.h"
#import <QuartzCore/QuartzCore.h>
@implementation LayerLabel
+ ( Class )layerClass
{
//this makes our label create a CATextLayer
//instead of a regular CALayer for its backing layer
return [ CATextLayer class ];
}
- ( CATextLayer *)textLayer
{
return ( CATextLayer *) self . layer ;
}
- ( void )setUp
{
//set defaults from UILabel settings
self . text = self . text ;
self . textColor = self . textColor ;
self . font = self . font ;
//we should really derive these from the UILabel settings too
//but that's complicated, so for now we'll just hard-code them
[ self textLayer ]. alignmentMode = kCAAlignmentJustified ;
Search WWH ::




Custom Search