-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNSMutableAttributedString+Simple.m
210 lines (177 loc) · 5.9 KB
/
NSMutableAttributedString+Simple.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
208
209
210
//
// NSMutableAttributedString+Simple.m
// reocar_ios
//
// Created by penoty yu on 09/08/2017.
// Copyright © 2017 reocar. All rights reserved.
//
#import "NSMutableAttributedString+Simple.h"
@implementation NSString (Simple)
@dynamic setFont;
@dynamic setSystemFontSize;
@dynamic setBoldSystemFontSize;
@dynamic setFontNameAndSize;
@dynamic font;
@dynamic setColor;
@dynamic color;
@dynamic setColorWithRGB;
@dynamic setUnderlineStyle;
@dynamic underlineStyle;
@dynamic setUnderlineColor;
@dynamic underlineColor;
@dynamic setStrikelineStyle;
@dynamic strikelineStyle;
@dynamic setStrikelineColor;
@dynamic strikelineColor;
@dynamic setBackgroundColor;
@dynamic backgroundColor;
- (void)setAttributes:(NSMutableDictionary *)attributes {
objc_setAssociatedObject(self, "NSString_Attributes", attributes, OBJC_ASSOCIATION_RETAIN);
}
- (NSMutableDictionary *)attributes {
if (!objc_getAssociatedObject(self, "NSString_Attributes")) {
objc_setAssociatedObject(self, "NSString_Attributes", @{}.mutableCopy, OBJC_ASSOCIATION_RETAIN);
}
return (NSMutableDictionary *)objc_getAssociatedObject(self, "NSString_Attributes");
}
- (void)setAttributeWithName:(NSString *)attributeName value:(id)attributeValue {
NSMutableDictionary *attributes = [self attributes];
attributes[attributeName] = attributeValue;
[self setAttributes:attributes];
}
- (id)attributeValueWithName:(NSString *)attributeName {
return [self attributes][attributeName];
}
- (NSString *(^)(UIColor *color))setColor {
return ^(UIColor *color) {
[self setAttributeWithName:NSForegroundColorAttributeName value:color];
return self;
};
}
- (NSString *(^)(NSInteger rgb))setColorWithRGB {
return ^(NSInteger rgb) {
return self.setColor(kUIColorFromRGB(rgb));
};
}
- (UIColor *(^)())color {
return ^() {
return [self attributeValueWithName:NSForegroundColorAttributeName];
};
}
- (NSString *(^)(UIFont *font))setFont {
return ^(UIFont *font) {
[self setAttributeWithName:NSFontAttributeName value:font];
return self;
};
}
- (NSString *(^)(CGFloat size))setSystemFontSize {
return ^(CGFloat size) {
return self.setFont([UIFont systemFontOfSize:size]);
};
}
- (NSString *(^)(CGFloat size))setBoldSystemFontSize {
return ^(CGFloat size) {
return self.setFont([UIFont boldSystemFontOfSize:size]);
};
}
- (NSString *(^)(NSString *name, CGFloat size))setFontNameAndSize {
return ^(NSString *name, CGFloat size) {
return self.setFont([UIFont fontWithName:name size:size]);
};
}
- (UIFont *(^)())font {
return ^() {
return [self attributeValueWithName:NSFontAttributeName];
};
}
- (NSString *(^)(NSNumber * underlineStyle))setUnderlineStyle {
return ^(NSNumber * underlineStyle){
[self setAttributeWithName:NSUnderlineStyleAttributeName value:underlineStyle];
return self;
};
}
- (NSNumber * (^)())underlineStyle {
return ^() {
return [self attributeValueWithName:NSUnderlineStyleAttributeName];
};
}
- (NSString *(^)(UIColor *underlineColor))setUnderlineColor {
return ^(UIColor *underlineColor){
[self setAttributeWithName:NSUnderlineColorAttributeName value:underlineColor];
return self;
};
}
- (UIColor *(^)())underlineColor {
return ^(){
return [self attributeValueWithName:NSUnderlineColorAttributeName];
};
}
- (NSString *(^)(NSNumber *strikelineStyle))setStrikelineStyle {
return ^(NSNumber * strikelineStyle){
[self setAttributeWithName:NSStrikethroughStyleAttributeName value:strikelineStyle];
return self;
};
}
- (NSNumber * (^)())strikelineStyle {
return ^() {
return [self attributeValueWithName:NSStrikethroughStyleAttributeName];
};
}
- (NSString *(^)(UIColor *strikelineColor))setStrikelineColor {
return ^(UIColor *strikelineColor){
[self setAttributeWithName:NSStrikethroughColorAttributeName value:strikelineColor];
return self;
};
}
- (UIColor *(^)())strikelineColor {
return ^(){
return [self attributeValueWithName:NSStrikethroughColorAttributeName];
};
}
- (NSString *(^)(UIColor *backgroundColor))setBackgroundColor {
return ^(UIColor *backgroundColor){
[self setAttributeWithName:NSBackgroundColorAttributeName value:backgroundColor];
return self;
};
}
- (UIColor *(^)())backgroundColor {
return ^(){
return [self attributeValueWithName:NSBackgroundColorAttributeName];
};
}
@end
@implementation NSMutableString (Simple)
+ (instancetype)stringWithFormat:(NSString *)format strings:(NSArray *)strArray {
NSMutableString *mutableStr = [format mutableCopy];
for (NSString *str in strArray) {
NSRange range = [mutableStr rangeOfString:@"%@"];
if (range.location != NSNotFound) {
[mutableStr replaceCharactersInRange:range withString:str];
}
}
return mutableStr;
}
@end
@implementation NSMutableAttributedString (Simple)
+ (instancetype)stringWithFormat:(NSString *)format strings:(NSArray *)strArray {
@try {
NSMutableString *combinedStr = [NSMutableString stringWithFormat:format strings:strArray];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:combinedStr];
NSUInteger lastLoc = 0;
for (NSString *str in strArray) {
if (lastLoc == NSNotFound) { return attributedString; }
NSRange range = [combinedStr rangeOfString:str options:NSLiteralSearch range:NSMakeRange(lastLoc, combinedStr.length - lastLoc)];
if (range.location != NSNotFound) {
lastLoc = range.location + range.length;
[attributedString addAttributes:[str attributes] range:range];
}
}
return attributedString;
}
@catch (NSException *exception) {
return [[NSMutableAttributedString alloc] initWithString:@""];
}
@finally {
}
}
@end