Graphics Reference
In-Depth Information
Figure 6.4
A rich text label implemented using
CATextLayer
Leading and Kerning
It's worth mentioning that the
leading
(line spacing) and
kerning
(spacing between letters)
for text rendered using
CATextLayer
is not completely identical to that of the string
rendering used by
UILabel
due to the different drawing implementations (Core Text and
WebKit, respectively).
The extent of the discrepancy varies (depending on the specific font and characters used)
and is generally fairly minor, but you should keep this mind if you are trying to exactly
match appearance between regular labels and a
CATextLayer
.
A
UILabel
Replacement
We've established that
CATextLayer
has performance benefits over
UILabel
, as well
as some additional layout options and support for rich text on iOS 5. But it's fairly
cumbersome to use by comparison to a regular label. If we want to make a truly usable
replacement for
UILabel
, we should be able to create our labels in Interface Builder, and
they should behave as much as possible like regular views.
We could subclass
UILabel
and override its methods to display the text in a
CATextLayer
that we've added as a sublayer, but we'd still have the redundant empty
backing image created by the presence of
UILabel
-drawRect:
method. And because
CALayer
doesn't support autoresizing or autolayout, a sublayer wouldn't track the size of
the view
bounds
automatically, so we would need to manually update the sublayer bounds
every time the view is resized.