Graphics Reference
In-Depth Information
CATextLayer
also renders much faster than
UILabel
. It's a little-known fact that on
iOS6 and earlier,
UILabel
actually uses WebKit to do its text drawing, which carries a
significant performance overhead when you are drawing a lot of text.
CATextLayer
uses
Core Text and is significantly faster.
Let's try displaying some text using a
CATextLayer
. Listing 6.2 shows the code to set up
and display a
CATextLayer
, and Figure 6.2 shows the result.
Listing 6.2
Implementing a Text Label Using
CATextLayer
@interface
ViewController ()
@property
(
nonatomic
,
weak
)
IBOutlet
UIView
*labelView;
@end
@implementation
ViewController
- (
void
)viewDidLoad
{
[
super
viewDidLoad
];
//create a text layer
CATextLayer
*textLayer = [
CATextLayer
layer
];
textLayer.
frame
=
self
.labelView.
bounds
;
[
self
.labelView.
layer
addSublayer
:textLayer];
//set text attributes
textLayer.
foregroundColor
= [
UIColor
blackColor
].
CGColor
;
textLayer.
alignmentMode
=
kCAAlignmentJustified
;
textLayer.
wrapped
=
YES
;
//choose a font
UIFont
*font = [
UIFont
systemFontOfSize
:
15
];
//set layer font
CFStringRef
fontName = (
__bridge
CFStringRef
)font.
fontName
;
CGFontRef
fontRef =
CGFontCreateWithFontName
(fontName);
textLayer.
font
= fontRef;
textLayer.
fontSize
= font.
pointSize
;
CGFontRelease
(fontRef);
//choose some text
NSString
*text =
@"Lorem ipsum dolor sit amet, consectetur adipiscing \
elit. Quisque massa arcu, eleifend vel varius in, facilisis pulvinar \
leo. Nunc quis nunc at mauris pharetra condimentum ut ac neque. Nunc \