@@ -19,39 +19,80 @@ + (void)load {
1919 });
2020}
2121
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+
2236static char kAssociatedObjectKey_PlaceholderColor ;
2337- (void )setQmui_placeholderColor : (UIColor *)qmui_placeholderColor {
2438 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 ;
2742 }
2843}
2944
3045- (UIColor *)qmui_placeholderColor {
3146 return (UIColor *)objc_getAssociatedObject (self, &kAssociatedObjectKey_PlaceholderColor );
3247}
3348
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-
4249static char kAssociatedObjectKey_TextColor ;
4350- (void )setQmui_textColor : (UIColor *)qmui_textColor {
4451 objc_setAssociatedObject (self, &kAssociatedObjectKey_TextColor , qmui_textColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC );
45- self.textField .textColor = qmui_textColor;
52+ self.qmui_textField .textColor = qmui_textColor;
4653}
4754
4855- (UIColor *)qmui_textColor {
4956 return (UIColor *)objc_getAssociatedObject (self, &kAssociatedObjectKey_TextColor );
5057}
5158
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+
5277- (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+
5596 self.placeholder = @" 搜索" ;
5697 self.autocorrectionType = UITextAutocorrectionTypeNo;
5798 self.autocapitalizationType = UITextAutocapitalizationTypeNone;
@@ -90,9 +131,4 @@ - (void)qmui_styledAsQMUISearchBar {
90131 }
91132}
92133
93- - (UITextField *)textField {
94- UITextField *textField = [self valueForKey: @" searchField" ];
95- return textField;
96- }
97-
98134@end
0 commit comments