@@ -19,39 +19,80 @@ + (void)load {
19
19
});
20
20
}
21
21
22
+ - (void )qmui_setPlaceholder : (NSString *)placeholder {
23
+ [self qmui_setPlaceholder: placeholder];
24
+ if (self.qmui_placeholderColor || self.qmui_font ) {
25
+ NSMutableDictionary <NSString *, id > *attributes = [[NSMutableDictionary alloc ] init ];
26
+ if (self.qmui_placeholderColor ) {
27
+ attributes[NSForegroundColorAttributeName ] = self.qmui_placeholderColor ;
28
+ }
29
+ if (self.qmui_font ) {
30
+ attributes[NSFontAttributeName ] = self.qmui_font ;
31
+ }
32
+ self.qmui_textField .attributedPlaceholder = [[NSAttributedString alloc ] initWithString: placeholder attributes: attributes];
33
+ }
34
+ }
35
+
22
36
static char kAssociatedObjectKey_PlaceholderColor ;
23
37
- (void )setQmui_placeholderColor : (UIColor *)qmui_placeholderColor {
24
38
objc_setAssociatedObject (self, &kAssociatedObjectKey_PlaceholderColor , qmui_placeholderColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC );
25
- if (self.placeholder && qmui_placeholderColor) {
26
- self.textField .attributedPlaceholder = [[NSAttributedString alloc ] initWithString: self .placeholder attributes: @{NSForegroundColorAttributeName : qmui_placeholderColor}];
39
+ if (self.placeholder ) {
40
+ // 触发 setPlaceholder 里更新 placeholder 样式的逻辑
41
+ self.placeholder = self.placeholder ;
27
42
}
28
43
}
29
44
30
45
- (UIColor *)qmui_placeholderColor {
31
46
return (UIColor *)objc_getAssociatedObject (self, &kAssociatedObjectKey_PlaceholderColor );
32
47
}
33
48
34
- - (void )qmui_setPlaceholder : (NSString *)placeholder {
35
- [self qmui_setPlaceholder: placeholder];
36
- // placeholder的颜色
37
- if (self.qmui_placeholderColor ) {
38
- self.textField .attributedPlaceholder = [[NSAttributedString alloc ] initWithString: placeholder attributes: @{NSForegroundColorAttributeName : self.qmui_placeholderColor }];
39
- }
40
- }
41
-
42
49
static char kAssociatedObjectKey_TextColor ;
43
50
- (void )setQmui_textColor : (UIColor *)qmui_textColor {
44
51
objc_setAssociatedObject (self, &kAssociatedObjectKey_TextColor , qmui_textColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC );
45
- self.textField .textColor = qmui_textColor;
52
+ self.qmui_textField .textColor = qmui_textColor;
46
53
}
47
54
48
55
- (UIColor *)qmui_textColor {
49
56
return (UIColor *)objc_getAssociatedObject (self, &kAssociatedObjectKey_TextColor );
50
57
}
51
58
59
+ static char kAssociatedObjectKey_font ;
60
+ - (void )setQmui_font : (UIFont *)qmui_font {
61
+ objc_setAssociatedObject (self, &kAssociatedObjectKey_font , qmui_font, OBJC_ASSOCIATION_RETAIN_NONATOMIC );
62
+ if (self.placeholder ) {
63
+ // 触发 setPlaceholder 里更新 placeholder 样式的逻辑
64
+ self.placeholder = self.placeholder ;
65
+ }
66
+ }
67
+
68
+ - (UIFont *)qmui_font {
69
+ return (UIFont *)objc_getAssociatedObject (self, &kAssociatedObjectKey_font );
70
+ }
71
+
72
+ - (UITextField *)qmui_textField {
73
+ UITextField *textField = [self valueForKey: @" searchField" ];
74
+ return textField;
75
+ }
76
+
52
77
- (void )qmui_styledAsQMUISearchBar {
53
- self.qmui_textColor = SearchBarTextColor;
54
- self.qmui_placeholderColor = SearchBarPlaceholderColor;
78
+ // 搜索框的字号及 placeholder 的字号
79
+ UIFont *font = SearchBarFont;
80
+ if (font) {
81
+ self.qmui_font = font;
82
+ }
83
+
84
+ // 搜索框的文字颜色
85
+ UIColor *textColor = SearchBarTextColor;
86
+ if (textColor) {
87
+ self.qmui_textColor = SearchBarTextColor;
88
+ }
89
+
90
+ // placeholder 的文字颜色
91
+ UIColor *placeholderColor = SearchBarPlaceholderColor;
92
+ if (placeholderColor) {
93
+ self.qmui_placeholderColor = SearchBarPlaceholderColor;
94
+ }
95
+
55
96
self.placeholder = @" 搜索" ;
56
97
self.autocorrectionType = UITextAutocorrectionTypeNo;
57
98
self.autocapitalizationType = UITextAutocapitalizationTypeNone;
@@ -90,9 +131,4 @@ - (void)qmui_styledAsQMUISearchBar {
90
131
}
91
132
}
92
133
93
- - (UITextField *)textField {
94
- UITextField *textField = [self valueForKey: @" searchField" ];
95
- return textField;
96
- }
97
-
98
134
@end
0 commit comments