-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTLStyledTextFragment.m
114 lines (91 loc) · 2.97 KB
/
TLStyledTextFragment.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
//
// TLStyledTextFragment.m
// TLCommon
//
// Created by Joshua Bleecher Snyder on 10/26/09.
//
#import "TLStyledTextFragment.h"
#import "TLStyledTextStyle.h"
#import "CGContext_TLCommon.h"
#import "NSString_TLCommon.h"
#pragma mark -
@interface TLStyledTextFragment ()
@end
#pragma mark -
@implementation TLStyledTextFragment
@synthesize text;
@synthesize style;
@synthesize userInfo;
@synthesize renderRect;
+ (TLStyledTextFragment *)fragment {
return [[[self alloc] init] autorelease];
}
- (void)render {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
if(self.style.backgroundColor) {
CGContextSaveGState(context);
[self.style.backgroundColor setFill];
CGContextBeginPath(context);
CGContextAddRoundedRectToPath(context, self.renderRect, self.style.backgroundCornerRadius, self.style.backgroundCornerRadius);
CGContextClosePath(context);
CGContextClip(context);
UIRectFill(self.renderRect);
CGContextRestoreGState(context);
}
if(self.style.textColor) {
[self.style.textColor setFill];
}
CGSize renderedTextSize = [self.text drawInRect:self.renderRect withFont:self.style.font];
if(self.style.underlined) {
UIRectFill(CGRectMake(self.renderRect.origin.x,
CGRectGetMaxY(self.renderRect) - 1.0f,
renderedTextSize.width,
1.0f));
}
CGContextRestoreGState(context);
}
- (CGFloat)width {
return [self.text sizeWithFont:self.style.font].width;
}
- (NSArray *)subfragmentsRenderableOnLinesOfWidth:(CGFloat)lineWidth
firstLineWidth:(CGFloat)firstLineWidth {
NSMutableArray *fragments = [NSMutableArray array];
BOOL firstLine = YES;
NSString *subtext = self.text;
while([subtext length] > 0) {
CGFloat width = firstLine ? firstLineWidth : lineWidth;
NSUInteger prefixLength = [subtext lengthOfLongestPrefixThatRendersOnOneLineOfWidth:width
usingFont:self.style.font];
if(prefixLength == 0) {
if(!firstLine) {
// uh-oh...bail
subtext = @"";
} else {
[fragments addObject:[NSNull null]];
}
} else {
TLStyledTextFragment *newFragment = [TLStyledTextFragment fragment];
newFragment.style = self.style;
newFragment.text = [subtext substringToIndex:prefixLength];
newFragment.userInfo = self.userInfo;
[fragments addObject:newFragment];
subtext = [subtext substringFromIndex:prefixLength];
}
firstLine = NO;
}
return fragments;
}
- (NSString *)description {
return [NSString stringWithFormat:@"<%@ %p: '%@', %@, %@ -- %@>", NSStringFromClass([self class]), self, self.text, self.style, [NSValue valueWithCGRect:self.renderRect], self.userInfo];
}
- (void)dealloc {
[text release];
text = nil;
[style release];
style = nil;
[userInfo release];
userInfo = nil;
[super dealloc];
}
@end