Graphics Reference
In-Depth Information
attributes, but because the point of the exercise is to demonstrate that this feature also
works on iOS 5 and below, we've used the Core Text equivalents instead. This means that
you'll need to add the Core Text framework to your project; otherwise, the compiler won't
recognize the attribute constants.
Figure 6.4 shows the result. (Note the red, underlined text.)
Listing 6.3
Implementing a Rich Text Label Using
NSAttributedString
#import
"DrawingView.h"
#import
<QuartzCore/QuartzCore.h>
#import
<CoreText/CoreText.h>
@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
;
textLayer.
contentsScale
= [
UIScreen
mainScreen
].
scale
;
[
self
.
labelView
.
layer
addSublayer
:textLayer];
//set text attributes
textLayer.
alignmentMode
=
kCAAlignmentJustified
;
textLayer.
wrapped
=
YES
;
//choose a font
UIFont
*font = [
UIFont
systemFontOfSize
:
15
];
//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 \
elementum, libero ut porttitor dictum, diam odio congue lacus, vel \
fringilla sapien diam at purus. Etiam suscipit pretium nunc sit amet \
lobortis"
;