-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIAttributedLabel.m
216 lines (178 loc) · 8.13 KB
/
UIAttributedLabel.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//
// UIAttributedLabel.m
// Test-SandBox
//
// Created by Olivier on 20/07/10.
// Copyright 2010 AliSoftware. All rights reserved.
//
#import "UIAttributedLabel.h"
/////////////////////////////////////////////////////////////////////////////
// MARK: -
// MARK: NSMutableAttributedString Additions
/////////////////////////////////////////////////////////////////////////////
@implementation NSAttributedString (CommodityConstructors)
+(id)attributedStringWithString:(NSString*)string {
return [[[self alloc] initWithString:string] autorelease];
}
+(id)attributedStringWithAttributedString:(NSAttributedString*)attrStr {
return [[[self alloc] initWithAttributedString:attrStr] autorelease];
}
@end
@implementation NSMutableAttributedString (CommodityStyleModifiers)
-(void)setFont:(UIFont*)font {
[self setFontName:font.fontName size:font.pointSize];
}
-(void)setFont:(UIFont*)font range:(NSRange)range {
[self setFontName:font.fontName size:font.pointSize range:range];
}
-(void)setFontName:(NSString*)fontName size:(CGFloat)size {
[self setFontName:fontName size:size range:NSMakeRange(0,[self length])];
}
-(void)setFontName:(NSString*)fontName size:(CGFloat)size range:(NSRange)range {
// kCTFontAttributeName
CTFontRef aFont = CTFontCreateWithName((CFStringRef)fontName, size, NULL);
[self addAttribute:(NSString*)kCTFontAttributeName value:(id)aFont range:range];
}
-(void)setFontFamily:(NSString*)fontFamily size:(CGFloat)size bold:(BOOL)isBold italic:(BOOL)isItalic range:(NSRange)range {
// kCTFontFamilyNameAttribute + kCTFontTraitsAttribute
CTFontSymbolicTraits symTrait = (isBold?kCTFontBoldTrait:0) | (isItalic?kCTFontItalicTrait:0);
NSDictionary* trait = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:symTrait] forKey:(NSString*)kCTFontSymbolicTrait];
NSDictionary* attr = [NSDictionary dictionaryWithObjectsAndKeys:
fontFamily,kCTFontFamilyNameAttribute,
trait,kCTFontTraitsAttribute,nil];
CTFontDescriptorRef desc = CTFontDescriptorCreateWithAttributes((CFDictionaryRef)attr);
CTFontRef aFont = CTFontCreateWithFontDescriptor(desc, size, NULL);
[self addAttribute:(NSString*)kCTFontAttributeName value:(id)aFont range:range];
}
-(void)setTextColor:(UIColor*)color {
[self setTextColor:color range:NSMakeRange(0,[self length])];
}
-(void)setTextColor:(UIColor*)color range:(NSRange)range {
// kCTForegroundColorAttributeName
[self addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)color.CGColor range:range];
}
-(void)setTextAlignment:(CTTextAlignment)alignment lineBreakMode:(CTLineBreakMode)lineBreakMode {
[self setTextAlignment:alignment lineBreakMode:lineBreakMode range:NSMakeRange(0,[self length])];
}
-(void)setTextAlignment:(CTTextAlignment)alignment lineBreakMode:(CTLineBreakMode)lineBreakMode range:(NSRange)range {
// kCTParagraphStyleAttributeName > kCTParagraphStyleSpecifierAlignment
CTParagraphStyleSetting paraStyles[2] = {
{.spec = kCTParagraphStyleSpecifierAlignment, .valueSize = sizeof(CTTextAlignment), .value = (const void*)&alignment},
{.spec = kCTParagraphStyleSpecifierLineBreakMode, .valueSize = sizeof(CTLineBreakMode), .value = (const void*)&lineBreakMode},
};
CTParagraphStyleRef aStyle = CTParagraphStyleCreate(paraStyles, 2);
[self addAttribute:(NSString*)kCTParagraphStyleAttributeName value:(id)aStyle range:range];
}
@end
/////////////////////////////////////////////////////////////////////////////
// MARK: -
// MARK: UIAttributedLabel
/////////////////////////////////////////////////////////////////////////////
CTTextAlignment CTTextAlignmentFromUITextAlignment(UITextAlignment alignment) {
switch (alignment) {
case UITextAlignmentLeft: return kCTLeftTextAlignment;
case UITextAlignmentCenter: return kCTCenterTextAlignment;
case UITextAlignmentRight: return kCTRightTextAlignment;
case UITextAlignmentJustify: return kCTJustifiedTextAlignment; /* special OOB value if we decide to use it even if it's not really standard... */
default: return kCTNaturalTextAlignment;
}
}
CTLineBreakMode CTLineBreakModeFromUILineBreakMode(UILineBreakMode lineBreakMode) {
switch (lineBreakMode) {
case UILineBreakModeWordWrap: return kCTLineBreakByWordWrapping;
case UILineBreakModeCharacterWrap: return kCTLineBreakByCharWrapping;
case UILineBreakModeClip: return kCTLineBreakByClipping;
case UILineBreakModeHeadTruncation: return kCTLineBreakByTruncatingHead;
case UILineBreakModeTailTruncation: return kCTLineBreakByTruncatingTail;
case UILineBreakModeMiddleTruncation: return kCTLineBreakByTruncatingMiddle;
default: return 0;
}
}
/////////////////////////////////////////////////////////////////////////////
@implementation UIAttributedLabel
- (id)initWithCoder:(NSCoder *)decoder
{
self = [super initWithCoder:decoder];
if (self != nil) {
[self resetAttributedText];
}
return self;
}
- (void)drawTextInRect:(CGRect)rect
{
if (_attributedText) {
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSaveGState(ctx);
CGContextConcatCTM(ctx, CGAffineTransformScale(CGAffineTransformMakeTranslation(0, self.bounds.size.height), 1.f, -1.f));
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, self.bounds); // self.bounds
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)_attributedText);
CTFrameRef frame = CTFramesetterCreateFrame(framesetter,CFRangeMake(0,0), path, NULL);
CFRelease(framesetter);
CTFrameDraw(frame, ctx);
CFRelease(frame);
CGPathRelease(path);
CGContextRestoreGState(ctx);
} else {
[super drawTextInRect:rect];
}
}
- (CGSize)sizeThatFits:(CGSize)size {
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)_attributedText);
CGFloat w = self.frame.size.width;
CGSize sz = CTFramesetterSuggestFrameSizeWithConstraints(framesetter,CFRangeMake(0,0),NULL,CGSizeMake(w,CGFLOAT_MAX),NULL);
CFRelease(framesetter);
return CGSizeMake(w,sz.height+1); // take 1pt of margin
}
/////////////////////////////////////////////////////////////////////////////
-(void)resetAttributedText {
NSMutableAttributedString* mutAttrStr = [NSMutableAttributedString attributedStringWithString:self.text];
[mutAttrStr setFont:self.font];
[mutAttrStr setTextColor:self.textColor];
CTTextAlignment coreTextAlign = CTTextAlignmentFromUITextAlignment(self.textAlignment);
CTLineBreakMode coreTextLBMode = CTLineBreakModeFromUILineBreakMode(self.lineBreakMode);
[mutAttrStr setTextAlignment:coreTextAlign lineBreakMode:coreTextLBMode];
self.attributedText = mutAttrStr;
}
-(NSAttributedString*)attributedText {
if (!_attributedText) {
[self resetAttributedText];
}
return [[_attributedText copy] autorelease]; // immutable autoreleased copy
}
-(void)setAttributedText:(NSAttributedString*)attributedText {
[_attributedText release];
_attributedText = [attributedText mutableCopy];
[self setNeedsDisplay];
}
/////////////////////////////////////////////////////////////////////////////
-(void)setText:(NSString *)text {
[super setText:text]; // will call setNeedsDisplay too
[self resetAttributedText];
}
-(void)setFont:(UIFont *)font {
[_attributedText setFont:font];
[super setFont:font]; // will call setNeedsDisplay too
}
-(void)setTextColor:(UIColor *)color {
[_attributedText setTextColor:color];
[super setTextColor:color]; // will call setNeedsDisplay too
}
-(void)setTextAlignment:(UITextAlignment)alignment {
CTTextAlignment coreTextAlign = CTTextAlignmentFromUITextAlignment(alignment);
CTLineBreakMode coreTextLBMode = CTLineBreakModeFromUILineBreakMode(self.lineBreakMode);
[_attributedText setTextAlignment:coreTextAlign lineBreakMode:coreTextLBMode];
[super setTextAlignment:alignment]; // will call setNeedsDisplay too
}
-(void)setLineBreakMode:(UILineBreakMode)lineBreakMode {
CTTextAlignment coreTextAlign = CTTextAlignmentFromUITextAlignment(self.textAlignment);
CTLineBreakMode coreTextLBMode = CTLineBreakModeFromUILineBreakMode(lineBreakMode);
[_attributedText setTextAlignment:coreTextAlign lineBreakMode:coreTextLBMode];
[super setLineBreakMode:lineBreakMode]; // will call setNeedsDisplay too
}
/////////////////////////////////////////////////////////////////////////////
-(void)dealloc {
[_attributedText release];
[super dealloc];
}
@end