Skip to content

Commit c7c7eb0

Browse files
author
teejay
committedMay 9, 2013
Attributed string support.
1 parent a898673 commit c7c7eb0

File tree

5 files changed

+269
-28
lines changed

5 files changed

+269
-28
lines changed
 

‎MFLHintLabel/MFLHintLabel/MFLHintLabel/MFLHintLabel.h

+8
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ typedef enum {
3333

3434
#warning Set these properties before calling prepareToRun
3535
@property (nonatomic, strong) NSString *stringToDisplay;
36+
@property (nonatomic, strong) NSAttributedString *attributedStringToDisplay;
37+
3638
@property (nonatomic, strong) NSMutableArray *labelArray;
3739

3840

@@ -111,6 +113,12 @@ typedef enum {
111113
endingAt:(CGPoint)endPoint
112114
inTargetView:(UIView*)view;
113115

116+
- (MFLHintLabel *)createHintAnimationForAttributedText:(NSAttributedString*)text
117+
beginningAt:(CGPoint)startPoint
118+
displayingAt:(CGPoint)displayPoint
119+
endingAt:(CGPoint)endPoint
120+
inTargetView:(UIView*)view;
121+
114122
- (MFLHintLabel *)createHintAnimationForLabel:(UILabel*)label
115123
beginningAt:(CGPoint)startPoint
116124
displayingAt:(CGPoint)displayPoint

‎MFLHintLabel/MFLHintLabel/MFLHintLabel/MFLHintLabel.m

+111-14
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,16 @@ - (MFLHintLabel *)createHintAnimationForLabel:(UILabel*)label
6060
endingAt:(CGPoint)endPoint
6161
inTargetView:(UIView*)view
6262
{
63+
6364
id animation = [self init];
6465

6566
_font = label.font;
66-
_stringToDisplay = label.text;
67+
if (label.attributedText) {
68+
_attributedStringToDisplay = label.attributedText;
69+
} else {
70+
_stringToDisplay = label.text;
71+
}
72+
6773
_alignment = label.textAlignment;
6874

6975
_startPosition = startPoint;
@@ -79,6 +85,28 @@ - (MFLHintLabel *)createHintAnimationForLabel:(UILabel*)label
7985
return animation;
8086
}
8187

88+
- (MFLHintLabel *)createHintAnimationForAttributedText:(NSAttributedString*)text
89+
beginningAt:(CGPoint)startPoint
90+
displayingAt:(CGPoint)displayPoint
91+
endingAt:(CGPoint)endPoint
92+
inTargetView:(UIView*)view
93+
{
94+
id animation = [self init];
95+
_attributedStringToDisplay = text;
96+
97+
_startPosition = startPoint;
98+
_displayPosition = displayPoint;
99+
_endPosition = endPoint;
100+
_targetView = view;
101+
102+
_alignment = NSTextAlignmentCenter;
103+
_textColor = [UIColor blackColor];
104+
[self setDefaultProperties];
105+
106+
return animation;
107+
}
108+
109+
82110
- (void)setDefaultProperties
83111
{
84112
_animateOffType = kMFLAnimateOffCurvedExplode;
@@ -103,10 +131,20 @@ - (void)prepareToRun
103131
UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.widthConstraint, 250)];
104132
[testLabel setTextAlignment:self.alignment];
105133
[testLabel setFont:self.font];
106-
[testLabel setText:self.stringToDisplay];
107-
_lineArray = [testLabel linesForWidth:self.widthConstraint];
134+
if (self.attributedStringToDisplay) {
135+
[testLabel setAttributedText:self.attributedStringToDisplay];
136+
}else{
137+
[testLabel setText:self.stringToDisplay];
138+
}
108139

109-
[self createLabels];
140+
self.lineArray = [testLabel linesForWidth:self.widthConstraint];
141+
142+
if ([self.lineArray[0] isKindOfClass:[NSAttributedString class]]) {
143+
[self constructAttributedLabels];
144+
} else {
145+
[self createStringLabels];
146+
}
147+
110148

111149
}
112150

@@ -144,14 +182,13 @@ - (void)runWithCompletion:(void (^)())animEndsBlock
144182
[self run];
145183
}
146184

147-
- (void)createLabels
185+
- (void)createStringLabels
148186
{
149-
150187
CGFloat xOffset = self.startPosition.x;
151188
CGFloat yOffset = self.startPosition.y;
152189
self.labelArray = [@[] mutableCopy];
153190

154-
for (NSString *line in self.lineArray) {
191+
for (NSString* line in self.lineArray) {
155192
NSMutableArray *lineLabels = [@[] mutableCopy];
156193

157194
if (self.alignment == NSTextAlignmentCenter) {
@@ -190,6 +227,50 @@ - (void)createLabels
190227
}
191228
}
192229

230+
- (void)constructAttributedLabels
231+
{
232+
CGFloat xOffset = self.startPosition.x;
233+
CGFloat yOffset = self.startPosition.y;
234+
self.labelArray = [@[] mutableCopy];
235+
236+
for (NSAttributedString* line in self.lineArray) {
237+
NSMutableArray *lineLabels = [@[] mutableCopy];
238+
239+
if (self.alignment == NSTextAlignmentCenter) {
240+
CGSize lineSize = line.size;
241+
xOffset = self.startPosition.x + ((self.widthConstraint - lineSize.width)/2) ;
242+
}else{
243+
xOffset = self.startPosition.x;
244+
}
245+
246+
for (int i = 0; i < line.length; i++) {
247+
248+
NSAttributedString*character = [line attributedSubstringFromRange:NSMakeRange(i, 1)];
249+
CGSize characterSize = character.size;
250+
251+
UILabel *characterLabel = [[UILabel alloc]initWithFrame:CGRectMake(xOffset,
252+
yOffset,
253+
characterSize.width,
254+
characterSize.height)];
255+
256+
[characterLabel setBackgroundColor:[UIColor clearColor]];
257+
[characterLabel setAttributedText:character];
258+
[lineLabels addObject:characterLabel];
259+
[self.targetView addSubview:characterLabel];
260+
261+
if (self.shouldFade) {
262+
characterLabel.alpha = 0;
263+
}
264+
265+
xOffset += characterSize.width + self.tweakKerning;
266+
}
267+
268+
[self.labelArray addObject:lineLabels];
269+
yOffset += line.size.height + self.tweakLineheight;
270+
}
271+
}
272+
273+
193274
#pragma mark Animation Methods
194275

195276
- (CGFloat)animateToPosition:(CGPoint)position
@@ -362,7 +443,13 @@ - (CGFloat)implodeOntoScreen
362443

363444
for (UILabel *character in line) {
364445

365-
CGSize characterSize = [character.text sizeWithFont:self.font];
446+
CGSize characterSize;
447+
if (character.attributedText) {
448+
characterSize = character.attributedText.size;
449+
}else{
450+
characterSize = [character.text sizeWithFont:self.font];
451+
}
452+
366453

367454
[UIView animateWithDuration:self.duration delay:.3 options:self.options animations:^{
368455
character.frame = CGRectMake(xOffset,
@@ -691,9 +778,14 @@ - (CGFloat)solitareToExitRandomly
691778
character.frame.size.height)];
692779
[trail setCenter:point];
693780

694-
[trail setText:character.text];
695-
[trail setTextColor:character.textColor];
696-
[trail setFont:character.font];
781+
if (character.attributedText) {
782+
[trail setAttributedText:character.attributedText];
783+
} else {
784+
[trail setText:character.text];
785+
[trail setTextColor:character.textColor];
786+
[trail setFont:character.font];
787+
}
788+
697789
[trail setBackgroundColor:[UIColor clearColor]];
698790
[trailArray addObject:trail];
699791

@@ -802,9 +894,14 @@ - (CGFloat)solitareToExit
802894
character.frame.size.height)];
803895
[trail setCenter:point];
804896

805-
[trail setText:character.text];
806-
[trail setTextColor:character.textColor];
807-
[trail setFont:character.font];
897+
if (character.attributedText) {
898+
[trail setAttributedText:character.attributedText];
899+
} else {
900+
[trail setText:character.text];
901+
[trail setTextColor:character.textColor];
902+
[trail setFont:character.font];
903+
}
904+
808905
[trail setBackgroundColor:[UIColor clearColor]];
809906
[trailArray addObject:trail];
810907

‎MFLHintLabel/MFLHintLabel/MFLHintLabel/UILabel+Lines.m

+30-12
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,20 @@ @implementation UILabel (Lines)
1515

1616
- (NSArray*) linesForWidth:(CGFloat)width
1717
{
18-
NSString *text = self.text;
18+
1919
UIFont *font = self.font;
2020
CGRect rect = self.frame;
2121

22-
CTFontRef myFont = CTFontCreateWithName((__bridge CFStringRef)([font fontName]), [font pointSize], NULL);
23-
NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:text];
24-
[attStr addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)myFont range:NSMakeRange(0, attStr.length)];
22+
NSMutableAttributedString *attStr;
23+
if (self.attributedText) {
24+
attStr = [self.attributedText mutableCopy];
25+
} else {
26+
attStr = [[NSMutableAttributedString alloc] initWithString:self.text];
27+
CTFontRef myFont = CTFontCreateWithName((__bridge CFStringRef)([font fontName]), [font pointSize], NULL);
28+
[attStr addAttribute:(NSString *)kCTFontAttributeName
29+
value:(__bridge id)myFont
30+
range:NSMakeRange(0, attStr.length)];
31+
}
2532

2633

2734
CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)attStr);
@@ -34,14 +41,25 @@ - (NSArray*) linesForWidth:(CGFloat)width
3441
NSArray *lines = (__bridge NSArray *)CTFrameGetLines(frame);
3542
NSMutableArray *linesArray = [[NSMutableArray alloc]init];
3643

37-
for (id line in lines)
38-
{
39-
CTLineRef lineRef = (__bridge CTLineRef )line;
40-
CFRange lineRange = CTLineGetStringRange(lineRef);
41-
NSRange range = NSMakeRange(lineRange.location, lineRange.length);
42-
43-
NSString *lineString = [text substringWithRange:range];
44-
[linesArray addObject:lineString];
44+
if (self.attributedText) {
45+
for (id line in lines)
46+
{
47+
CTLineRef lineRef = (__bridge CTLineRef )line;
48+
CFRange lineRange = CTLineGetStringRange(lineRef);
49+
NSRange range = NSMakeRange(lineRange.location, lineRange.length);
50+
NSAttributedString *lineString = [self.attributedText attributedSubstringFromRange:range];
51+
[linesArray addObject:lineString];
52+
}
53+
}else{
54+
for (id line in lines)
55+
{
56+
CTLineRef lineRef = (__bridge CTLineRef )line;
57+
CFRange lineRange = CTLineGetStringRange(lineRef);
58+
NSRange range = NSMakeRange(lineRange.location, lineRange.length);
59+
60+
NSString *lineString = [self.text substringWithRange:range];
61+
[linesArray addObject:lineString];
62+
}
4563
}
4664

4765
return (NSArray *)linesArray;

‎MFLHintLabel/MFLHintLabel/MFLViewController.m

+38
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,44 @@ - (IBAction)runWithCompletionBlock:(id)sender
322322
}
323323

324324

325+
- (IBAction)attributedStringTest:(id)sender
326+
{
327+
NSMutableAttributedString *as = [[NSMutableAttributedString alloc] initWithString:@"The quick brown fox jumps over the lazy dog"];
328+
[as addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:16] range:NSMakeRange(0, 13)];
329+
[as addAttribute:NSFontAttributeName value:[UIFont italicSystemFontOfSize:24] range:NSMakeRange(13, 13)];
330+
[as addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:32] range:NSMakeRange(26, as.length-26)];
331+
332+
[self.hintAnimation stop];
333+
334+
self.hintAnimation = [[MFLHintLabel alloc] createHintAnimationForAttributedText:as
335+
beginningAt:CGPointMake(40, -250)
336+
displayingAt:CGPointMake(20, self.view.center.y)
337+
endingAt:CGPointMake(40, self.view.frame.size.height+200)
338+
inTargetView:self.view];
339+
[self.hintAnimation setAnimateOnType:kMFLAnimateOnLinear];
340+
[self.hintAnimation setAnimateOffType:kMFLAnimateImplodeStill];
341+
342+
[self.hintAnimation setImplodeWithinFrame:self.view.frame];
343+
344+
[self.hintAnimation setPhaseDelayTimeIn:.05];
345+
[self.hintAnimation setPhaseDelayTimeOut:.05];
346+
347+
[self.hintAnimation setCharactersToMoveSimultaneouslyIn:3];
348+
[self.hintAnimation setCharactersToMoveSimultaneouslyOut:5];
349+
350+
[self.hintAnimation prepareToRun];
351+
352+
[self.hintAnimation runWithCompletion:^{
353+
[[[UIAlertView alloc]initWithTitle:@"Completed"
354+
message:@"This was fired from the completion block"
355+
delegate:nil
356+
cancelButtonTitle:@"Cool!"
357+
otherButtonTitles:nil] show];
358+
}];
359+
360+
361+
}
362+
325363
- (void)didReceiveMemoryWarning
326364
{
327365
[super didReceiveMemoryWarning];

‎MFLHintLabel/MFLHintLabel/en.lproj/MFLViewController.xib

+82-2
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,29 @@
121121
<reference key="IBUIFontDescription" ref="604793775"/>
122122
<reference key="IBUIFont" ref="1031678221"/>
123123
</object>
124+
<object class="IBUIButton" id="361286904">
125+
<reference key="NSNextResponder" ref="774585933"/>
126+
<int key="NSvFlags">292</int>
127+
<string key="NSFrame">{{12, 181}, {90, 30}}</string>
128+
<reference key="NSSuperview" ref="774585933"/>
129+
<reference key="NSWindow"/>
130+
<reference key="NSNextKeyView"/>
131+
<string key="NSReuseIdentifierKey">_NS:9</string>
132+
<bool key="IBUIOpaque">NO</bool>
133+
<string key="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
134+
<int key="IBUIContentHorizontalAlignment">0</int>
135+
<int key="IBUIContentVerticalAlignment">0</int>
136+
<string key="IBUINormalTitle">Attributed</string>
137+
<reference key="IBUIHighlightedTitleColor" ref="554446740"/>
138+
<object class="NSColor" key="IBUINormalTitleColor">
139+
<int key="NSColorSpace">3</int>
140+
<bytes key="NSWhite">MQA</bytes>
141+
<reference key="NSCustomColorSpace" ref="217398414"/>
142+
</object>
143+
<reference key="IBUINormalTitleShadowColor" ref="235045675"/>
144+
<reference key="IBUIFontDescription" ref="604793775"/>
145+
<reference key="IBUIFont" ref="1031678221"/>
146+
</object>
124147
<object class="IBUIButton" id="272873383">
125148
<reference key="NSNextResponder" ref="774585933"/>
126149
<int key="NSvFlags">292</int>
@@ -151,7 +174,7 @@
151174
<string key="NSFrame">{{217, 143}, {90, 30}}</string>
152175
<reference key="NSSuperview" ref="774585933"/>
153176
<reference key="NSWindow"/>
154-
<reference key="NSNextKeyView"/>
177+
<reference key="NSNextKeyView" ref="361286904"/>
155178
<string key="NSReuseIdentifierKey">_NS:9</string>
156179
<bool key="IBUIOpaque">NO</bool>
157180
<string key="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
@@ -477,6 +500,16 @@
477500
</object>
478501
<int key="connectionID">222</int>
479502
</object>
503+
<object class="IBConnectionRecord">
504+
<object class="IBCocoaTouchOutletCollectionConnection" key="connection">
505+
<string key="label">buttons</string>
506+
<reference key="source" ref="372490531"/>
507+
<reference key="destination" ref="361286904"/>
508+
<string key="cachedDesigntimeCollectionClassName">NSArray</string>
509+
<bool key="addsContentToExistingCollection">NO</bool>
510+
</object>
511+
<int key="connectionID">227</int>
512+
</object>
480513
<object class="IBConnectionRecord">
481514
<object class="IBCocoaTouchEventConnection" key="connection">
482515
<string key="label">fireCurve:</string>
@@ -585,6 +618,15 @@
585618
</object>
586619
<int key="connectionID">223</int>
587620
</object>
621+
<object class="IBConnectionRecord">
622+
<object class="IBCocoaTouchEventConnection" key="connection">
623+
<string key="label">attributedStringTest:</string>
624+
<reference key="source" ref="361286904"/>
625+
<reference key="destination" ref="372490531"/>
626+
<int key="IBEventType">7</int>
627+
</object>
628+
<int key="connectionID">226</int>
629+
</object>
588630
</array>
589631
<object class="IBMutableOrderedSet" key="objectRecords">
590632
<array key="orderedObjects">
@@ -621,6 +663,7 @@
621663
<reference ref="319325139"/>
622664
<reference ref="272873383"/>
623665
<reference ref="163973234"/>
666+
<reference ref="361286904"/>
624667
</array>
625668
<reference key="parent" ref="0"/>
626669
</object>
@@ -704,6 +747,12 @@
704747
<reference key="parent" ref="774585933"/>
705748
<string key="objectName">End w/ Block</string>
706749
</object>
750+
<object class="IBObjectRecord">
751+
<int key="objectID">224</int>
752+
<reference key="object" ref="361286904"/>
753+
<reference key="parent" ref="774585933"/>
754+
<string key="objectName">Attributed</string>
755+
</object>
707756
</array>
708757
</object>
709758
<dictionary class="NSMutableDictionary" key="flattenedProperties">
@@ -718,6 +767,7 @@
718767
<string key="210.IBPluginDependency">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
719768
<string key="212.IBPluginDependency">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
720769
<string key="220.IBPluginDependency">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
770+
<string key="224.IBPluginDependency">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
721771
<string key="29.IBPluginDependency">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
722772
<string key="55.IBPluginDependency">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
723773
<string key="6.IBPluginDependency">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
@@ -729,7 +779,7 @@
729779
<nil key="activeLocalization"/>
730780
<dictionary class="NSMutableDictionary" key="localizations"/>
731781
<nil key="sourceID"/>
732-
<int key="maxID">223</int>
782+
<int key="maxID">227</int>
733783
</object>
734784
<object class="IBClassDescriber" key="IBDocument.Classes">
735785
<array class="NSMutableArray" key="referencedPartialClassDescriptions">
@@ -738,22 +788,40 @@
738788
<string key="superclassName">UIViewController</string>
739789
<dictionary class="NSMutableDictionary" key="actions">
740790
<string key="animateOffAnimation:">id</string>
791+
<string key="animateOnAnimation:">id</string>
792+
<string key="attributedStringTest:">id</string>
741793
<string key="fireCurve:">id</string>
794+
<string key="fireExplode:">id</string>
742795
<string key="fireImplode:">id</string>
743796
<string key="fireImplodeFactorStill:">id</string>
744797
<string key="fireImplodeFrameStill:">id</string>
798+
<string key="fireLinear:">id</string>
745799
<string key="fireRandomCurve:">id</string>
746800
<string key="fireRandomSolitare:">id</string>
801+
<string key="fireSolitare:">id</string>
802+
<string key="runWithCompletionBlock:">id</string>
747803
</dictionary>
748804
<dictionary class="NSMutableDictionary" key="actionInfosByName">
749805
<object class="IBActionInfo" key="animateOffAnimation:">
750806
<string key="name">animateOffAnimation:</string>
751807
<string key="candidateClassName">id</string>
752808
</object>
809+
<object class="IBActionInfo" key="animateOnAnimation:">
810+
<string key="name">animateOnAnimation:</string>
811+
<string key="candidateClassName">id</string>
812+
</object>
813+
<object class="IBActionInfo" key="attributedStringTest:">
814+
<string key="name">attributedStringTest:</string>
815+
<string key="candidateClassName">id</string>
816+
</object>
753817
<object class="IBActionInfo" key="fireCurve:">
754818
<string key="name">fireCurve:</string>
755819
<string key="candidateClassName">id</string>
756820
</object>
821+
<object class="IBActionInfo" key="fireExplode:">
822+
<string key="name">fireExplode:</string>
823+
<string key="candidateClassName">id</string>
824+
</object>
757825
<object class="IBActionInfo" key="fireImplode:">
758826
<string key="name">fireImplode:</string>
759827
<string key="candidateClassName">id</string>
@@ -766,6 +834,10 @@
766834
<string key="name">fireImplodeFrameStill:</string>
767835
<string key="candidateClassName">id</string>
768836
</object>
837+
<object class="IBActionInfo" key="fireLinear:">
838+
<string key="name">fireLinear:</string>
839+
<string key="candidateClassName">id</string>
840+
</object>
769841
<object class="IBActionInfo" key="fireRandomCurve:">
770842
<string key="name">fireRandomCurve:</string>
771843
<string key="candidateClassName">id</string>
@@ -774,6 +846,14 @@
774846
<string key="name">fireRandomSolitare:</string>
775847
<string key="candidateClassName">id</string>
776848
</object>
849+
<object class="IBActionInfo" key="fireSolitare:">
850+
<string key="name">fireSolitare:</string>
851+
<string key="candidateClassName">id</string>
852+
</object>
853+
<object class="IBActionInfo" key="runWithCompletionBlock:">
854+
<string key="name">runWithCompletionBlock:</string>
855+
<string key="candidateClassName">id</string>
856+
</object>
777857
</dictionary>
778858
<dictionary class="NSMutableDictionary" key="outlets">
779859
<string key="onlyOffButton">UIButton</string>

0 commit comments

Comments
 (0)
Please sign in to comment.