forked from JohnSundell/SuperSpriteKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SSKMultiLineLabelNode.m
executable file
·207 lines (162 loc) · 6.96 KB
/
SSKMultiLineLabelNode.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
#import "SSKMultiLineLabelNode.h"
static CGFloat SSKMultiLineLabelNodeGetFontLineHeight(SSKFontType *font)
{
#if TARGET_OS_IPHONE
return font.lineHeight;
#else
return [[NSLayoutManager new] defaultLineHeightForFont:font];
#endif
}
static NSString *SSKMultiLineLabelNodeGetStringForWord(NSString *word)
{
return [word stringByAppendingString:@" "];
}
@interface SSKMultiLineLabelNode()
@property (nonatomic, strong) NSArray *lineLabelNodes;
@property (nonatomic) NSUInteger numberOfLines;
@property (nonatomic) CGFloat lineHeightMultiplier;
@property (nonatomic) CGFloat maximumWidth;
@end
@implementation SSKMultiLineLabelNode
+ (instancetype)multiLineLabelNodeWithFontNamed:(NSString *)fontName fontSize:(CGFloat)fontSize maximumWidth:(CGFloat)maximumWidth text:(NSString *)text
{
return [self multiLineLabelNodeWithFontNamed:fontName
fontSize:fontSize
fontColor:[SKColor blackColor]
numberOfLines:0
maximumWidth:maximumWidth
text:text];
}
+ (instancetype)multiLineLabelNodeWithFontNamed:(NSString *)fontName fontSize:(CGFloat)fontSize fontColor:(SKColor *)fontColor numberOfLines:(NSUInteger)numberOfLines maximumWidth:(CGFloat)maximumWidth text:(NSString *)text
{
return [self multiLineLabelNodeWithFontNamed:fontName
fontSize:fontSize
fontColor:[SKColor blackColor]
numberOfLines:0
lineHeightMultiplier:1
maximumWidth:maximumWidth
text:text];
}
+ (instancetype)multiLineLabelNodeWithFontNamed:(NSString *)fontName fontSize:(CGFloat)fontSize fontColor:(SKColor *)fontColor numberOfLines:(NSUInteger)numberOfLines lineHeightMultiplier:(CGFloat)lineHeightMultiplier maximumWidth:(CGFloat)maximumWidth text:(NSString *)text
{
SSKMultiLineLabelNode *label = [SSKMultiLineLabelNode node];
label.numberOfLines = numberOfLines;
label.lineHeightMultiplier = lineHeightMultiplier;
label.fontName = fontName;
label.fontSize = fontSize;
label.fontColor = fontColor;
label.maximumWidth = maximumWidth;
label.text = text;
return label;
}
- (void)setFontSize:(CGFloat)fontSize
{
if (_fontSize == fontSize) {
return;
}
_fontSize = fontSize;
[self drawLineLabelNodes];
}
- (void)setFontColor:(SKColor *)fontColor
{
if (!fontColor) {
return;
}
if ([_fontColor isEqual:fontColor]) {
return;
}
_fontColor = fontColor;
for (SKLabelNode *labelNode in self.lineLabelNodes) {
labelNode.fontColor = fontColor;
}
}
- (void)setText:(NSString *)text
{
if ([_text isEqualToString:text]) {
return;
}
_text = text;
[self drawLineLabelNodes];
}
- (void)drawLineLabelNodes
{
[self.lineLabelNodes makeObjectsPerformSelector:@selector(removeFromParent)];
self.lineLabelNodes = nil;
if ([self.text length] == 0 || self.maximumWidth <= 0) {
return;
}
NSString *text = [self.text stringByReplacingOccurrencesOfString:@"\n" withString:@"\n "];
NSMutableArray *lineLabelNodes = [NSMutableArray new];
NSArray *wordsInText = [text componentsSeparatedByString:@" "];
NSUInteger numberOfParsedWords = 0;
SSKFontType *font = [SSKFontType fontWithName:self.fontName size:self.fontSize];
CGFloat fontLineHeight = SSKMultiLineLabelNodeGetFontLineHeight(font);
fontLineHeight *= self.lineHeightMultiplier;
NSDictionary *textAttributes = @{NSFontAttributeName: font};
NSCharacterSet *newLineCharacterSet = [NSCharacterSet newlineCharacterSet];
while (numberOfParsedWords < [wordsInText count]) {
NSMutableString *lineText = [NSMutableString string];
while ([lineText sizeWithAttributes:textAttributes].width < self.maximumWidth) {
if (numberOfParsedWords == [wordsInText count]) {
break;
}
NSString *word = [wordsInText objectAtIndex:numberOfParsedWords];
NSArray *wordLines = [word componentsSeparatedByCharactersInSet:newLineCharacterSet];
if ([wordLines count] > 1) {
NSRange newLineCharacterRange = [word rangeOfCharacterFromSet:newLineCharacterSet];
word = [word stringByReplacingCharactersInRange:newLineCharacterRange withString:@""];
}
[lineText appendString:SSKMultiLineLabelNodeGetStringForWord(word)];
numberOfParsedWords++;
if ([wordLines count] > 1) {
break;
}
}
while ([lineText sizeWithAttributes:textAttributes].width > self.maximumWidth) {
if (numberOfParsedWords == 1) {
[lineText deleteCharactersInRange:NSMakeRange([lineText length] - 1, 1)];
NSString *suffix = @"...";
[lineText replaceCharactersInRange:NSMakeRange([lineText length] - [suffix length], [suffix length]) withString:suffix];
} else {
NSString *lastWord = SSKMultiLineLabelNodeGetStringForWord([wordsInText objectAtIndex:numberOfParsedWords - 1]);
NSRange deleteRange = NSMakeRange([lineText length] - [lastWord length], [lastWord length]);
[lineText deleteCharactersInRange:deleteRange];
numberOfParsedWords--;
}
}
SKLabelNode *lineLabelNode = [SKLabelNode labelNodeWithFontNamed:self.fontName];
lineLabelNode.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeLeft;
lineLabelNode.verticalAlignmentMode = SKLabelVerticalAlignmentModeBaseline;
lineLabelNode.fontSize = self.fontSize;
lineLabelNode.fontColor = self.fontColor;
lineLabelNode.text = lineText;
[self addChild:lineLabelNode];
[lineLabelNodes addObject:lineLabelNode];
if (self.numberOfLines > 0 && self.numberOfLines == [lineLabelNodes count]) {
break;
}
}
NSUInteger lineNumber = 1;
NSUInteger numberOfLines = [lineLabelNodes count];
for (SKLabelNode *labelNode in lineLabelNodes) {
CGPoint labelPosition;
labelPosition.x = 0;
labelPosition.y = (numberOfLines - lineNumber) * fontLineHeight;
labelNode.position = labelPosition;
lineNumber++;
}
self.lineLabelNodes = lineLabelNodes;
}
- (CGSize)size
{
CGSize size;
size.width = 0;
size.height = CGRectGetMaxY([[self.lineLabelNodes firstObject] frame]);
for (SKLabelNode *labelNode in self.lineLabelNodes) {
if (CGRectGetMaxX(labelNode.frame) > size.width) {
size.width = CGRectGetMaxX(labelNode.frame);
}
}
return size;
}
@end