Graphics Reference
In-Depth Information
Figure 6.3
The effect of setting the
contentsScale
to match the screen
The
CATextLayer
font
property is not a
UIFont
, it's a
CFTypeRef
. This allows you
to specify the font using either a
CGFontRef
or a
CTFontRef
(a Core Text font),
depending on your requirements. The font size is also set independently using the
fontSize
property, because
CTFontRef
and
CGFontRef
do not encapsulate the point
size like
UIFont
does. The example shows how to convert from a
UIFont
to a
CGFontRef
.
Also, the
CATextLayer
string
property is not an
NSString
as you might expect, but
is typed as
id
. This is to allow you the option of using an
NSAttributedString
instead of an
NSString
to specify the text (
NSAttributedString
is not a subclass of
NSString
). Attributed strings are the mechanism that iOS uses for rendering styled text.
They specify
style runs
, which are specific ranges of the string to which metadata such as
font, color, bold, italic, and so forth are attached.
Rich Text
In iOS 6, Apple added direct support for attributed strings to
UILabel
and to other UIKit
text views. This is a handy feature that makes attributed strings much easier to work with,
but
CATextLayer
has supported attributed strings since its introduction in iOS 3.2; so if
you still need to support earlier iOS versions with your app,
CATextLayer
is a great way
to add simple rich text labels to your interface without having to deal with the complexity
of Core Text or the hassle of using a
UIWebView
.
Let's modify the example to use an
NSAttributedString
(see Listing 6.3). On iOS 6
and above we could use the new
NSTextAttributeName
constants to set up our string