-
Notifications
You must be signed in to change notification settings - Fork 1
/
PlaceholderLabel.m
154 lines (122 loc) · 6.89 KB
/
PlaceholderLabel.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
//
// PlaceholderLabel.m
// CustomElements
//
// Created by Thiago Holanda on 11/8/15.
// Copyright © 2015 holanda.mobi. All rights reserved.
//
#import "PlaceholderLabel.h"
#import "NSDictionary+Custom.h"
NSString * const placeHolderFirst = @"{firstValue}";
NSString * const placeHolderSecond = @"{secondValue}";
NSString * const placeHolderThird = @"{thirdValue}";
@interface PlaceholderLabel() {
dispatch_once_t onceTokenViewLiveRendering;
}
@property (strong, nonatomic) NSMutableAttributedString *attributedStrings;
@end
@implementation PlaceholderLabel
-(void) prepareForInterfaceBuilder {
[self viewLiveRendering];
}
- (void) drawRect:(CGRect) rect {
[super drawRect: rect];
#ifndef TARGET_INTERFACE_BUILDER
[self viewLiveRendering];
#endif
}
- (void) viewLiveRendering {
dispatch_once(&onceTokenViewLiveRendering, ^{
NSMutableArray *items = [NSMutableArray new];
if (self.firstValue && ![self.firstValue isEqualToString:@""]) {
[items addObject:@{
@"value" : self.firstValue,
@"placeholder" : placeHolderFirst,
@"color" : self.firstColor ? self.firstColor : self.textColor,
@"bold" : @(self.firstBold),
@"height" : @(self.firstHeight),
@"customFont" : self.firstFont ? self.firstFont : @""
}];
}
if (self.secondValue && ![self.secondValue isEqualToString:@""]) {
[items addObject: @{
@"value" : self.secondValue,
@"placeholder" : placeHolderSecond,
@"color" : self.secondColor ? self.firstColor : self.textColor,
@"bold" : @(self.secondBold),
@"height" : @(self.secondHeight),
@"customFont" : self.secondFont ? self.secondFont : @""
}];
}
if (self.thirdValue && ![self.thirdValue isEqualToString:@""]) {
[items addObject: @{
@"value" : self.thirdValue,
@"placeholder" : placeHolderThird,
@"color" : self.thirdColor ? self.thirdColor : self.textColor,
@"bold" : @(self.thirdBold),
@"height" : @(self.thirdHeight),
@"customFont" : self.thirdFont ? self.thirdFont : @""
}];
}
// replace all placeholders to values
[items enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (![[obj objectForKey:@"value"] isEqualToString:@""])
self.text = [self.text stringByReplacingOccurrencesOfString: [obj valueForKey: @"placeholder"]
withString: [obj valueForKey: @"value"]];
}];
// apply the custom settings to each placeholder
self.attributedStrings = [[NSMutableAttributedString alloc] initWithString: self.text];
[items enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (![[obj valueForKey:@"value"] isEqualToString:@""]) {
NSRange valueRange = [self.text rangeOfString: [obj valueForKey: @"value"]];
if (valueRange.location != NSNotFound) {
NSNumber *height = [obj numberForKey:@"height"];
if (![[obj valueForKey: @"customFont"] isEqualToString:@""]) {
// the best way to do this kind of change is using the
// UIFontDescriptor, but until now, we are getting touch some issues
// to change attributes on UIFont. Now, we are use the customFont
// as property, but we need to change to UIFontDescriptor as soon
// as we can.
CGFloat pointSize = self.font.pointSize;
if (height)
pointSize = [height floatValue];
UIFont *customFont = [UIFont fontWithName: [obj valueForKey: @"customFont"]
size: [height floatValue]];
[self.attributedStrings addAttribute: NSFontAttributeName
value: customFont
range: valueRange];
}
else if ([[obj valueForKey: @"bold"] boolValue]) {
CGFloat pointSize = self.font.pointSize;
if (height)
pointSize = [height floatValue];
UIFontDescriptor *boldDescriptor = [[self.font fontDescriptor] fontDescriptorWithSymbolicTraits: UIFontDescriptorTraitBold];
UIFont *boldVersion = [UIFont fontWithDescriptor: boldDescriptor
size: pointSize];
[self.attributedStrings addAttribute: NSFontAttributeName
value: boldVersion
range: valueRange];
}
else if (height) {
UIFont *font = [UIFont fontWithName: self.font.fontName
size: [height floatValue]];
[self.attributedStrings addAttribute: NSFontAttributeName
value: font
range: valueRange];
}
if ([obj valueForKey: @"color"])
[self.attributedStrings addAttribute: NSForegroundColorAttributeName
value: [obj valueForKey: @"color"]
range: valueRange];
}
}
}];
if (self.attributedStrings && [self.attributedStrings length] > 0)
self.attributedText = self.attributedStrings;
});
}
- (void) forceRender {
onceTokenViewLiveRendering = 0;
[self viewLiveRendering];
}
@end