forked from LemonCake/MSLabel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMSLabel.m
198 lines (157 loc) · 4.98 KB
/
MSLabel.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
//
// MSLabel.m
// Miso
//
// Created by Joshua Wu on 11/15/11.
// Copyright (c) 2011 Miso. All rights reserved.
//
#import "MSLabel.h"
// small buffer to allow for characters like g,y etc
static const int kAlignmentBuffer = 5;
@interface MSLabel ()
- (void)setup;
- (NSArray *)stringsFromText:(NSString *)string;
@end
@implementation MSLabel
@synthesize lineHeight = _lineHeight;
@synthesize verticalAlignment = _verticalAlignment;
#pragma mark - Initilisation
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
[self setup];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if(self)
{
[self setup];
}
return self;
}
#pragma mark - Drawing
- (void)drawTextInRect:(CGRect)rect
{
NSArray *slicedStrings = [self stringsFromText:self.text];
if (self.highlighted) {
[self.highlightedTextColor set];
}
else {
[self.textColor set];
}
int numLines = slicedStrings.count;
if (numLines > self.numberOfLines && self.numberOfLines != 0)
{
numLines = self.numberOfLines;
}
int drawY = (self.frame.size.height / 2 - (_lineHeight * numLines) / 2) - kAlignmentBuffer;
for (int i = 0; i < numLines; i++)
{
NSString *line = [slicedStrings objectAtIndex:i];
// calculate draw Y based on alignment
switch (_verticalAlignment)
{
case MSLabelVerticalAlignmentTop:
{
drawY = i * _lineHeight;
}
break;
case MSLabelVerticalAlignmentMiddle:
{
if(i > 0)
{
drawY += _lineHeight;
}
}
break;
case MSLabelVerticalAlignmentBottom:
{
drawY = (self.frame.size.height - _lineHeight * numLines) + ((i * _lineHeight) - kAlignmentBuffer);
}
break;
default:
{
if(i > 0)
{
drawY += _lineHeight;
}
}
break;
}
// calculate draw X based on textAlignmentment
int drawX = 0;
if (self.textAlignment == UITextAlignmentCenter)
{
drawX = floorf((self.frame.size.width - [line sizeWithFont:self.font].width) / 2);
}
else if (self.textAlignment == UITextAlignmentRight)
{
drawX = (self.frame.size.width - [line sizeWithFont:self.font].width);
}
[line drawAtPoint:CGPointMake(drawX, drawY) forWidth:self.frame.size.width withFont:self.font fontSize:self.font.pointSize lineBreakMode:UILineBreakModeClip baselineAdjustment:UIBaselineAdjustmentNone];
}
}
#pragma mark - Properties
- (void)setLineHeight:(int)lineHeight
{
if (_lineHeight == lineHeight)
{
return;
}
_lineHeight = lineHeight;
[self setNeedsDisplay];
}
#pragma mark - Private Methods
- (void)setup
{
_lineHeight = 10;
_verticalAlignment = MSLabelVerticalAlignmentMiddle;
}
- (NSArray *)stringsFromText:(NSString *)string
{
NSMutableArray *stringsArray = [[[string componentsSeparatedByString:@" "] mutableCopy] autorelease];
NSMutableArray *slicedString = [NSMutableArray array];
while (stringsArray.count != 0)
{
NSString *line = @"";
NSMutableIndexSet *wordsToRemove = [NSMutableIndexSet indexSet];
for (int i = 0; i < [stringsArray count]; i++)
{
NSString *word = [stringsArray objectAtIndex:i];
if ([[line stringByAppendingFormat:@"%@ ", word] sizeWithFont:self.font].width <= self.frame.size.width)
{
line = [line stringByAppendingFormat:@"%@ ", word];
[wordsToRemove addIndex:i];
}
else
{
if (line.length == 0)
{
line = [line stringByAppendingFormat:@"%@ ", word];
[wordsToRemove addIndex:i];
}
break;
}
}
[slicedString addObject:[line stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
[stringsArray removeObjectsAtIndexes:wordsToRemove];
}
if (slicedString.count > self.numberOfLines && self.numberOfLines != 0)
{
NSString *line = [slicedString objectAtIndex:(self.numberOfLines - 1)];
if ([line length] >= 3) {
line = [line stringByReplacingCharactersInRange:NSMakeRange(line.length - 3, 3) withString:@"..."];
} else {
line = @"...";
}
[slicedString removeObjectAtIndex:(self.numberOfLines - 1)];
[slicedString insertObject:line atIndex:(self.numberOfLines - 1)];
}
return slicedString;
}
@end