UISearchBar change placeholder color

前端 未结 17 2437
南旧
南旧 2020-12-04 15:16

Has anyone any idea or code sample on how can I change the text color of the placeholder text of a UISearchBar?

17条回答
  •  日久生厌
    2020-12-04 15:49

    iOS 13

    Previous solutions may not work on iOS 13 because new searchTextField has been added, and you can set attributed string on it.

    I wrapped that into category:

    @interface UISearchBar (AttributtedSetter)
    - (void)setThemedPlaceholder:(NSString*)localizationKey;
    @end
    
    @implementation UISearchBar (AttributtedSetter)
    
    - (void)setThemedPlaceholder:(NSString*)localizationKey {
        ThemeObject *currentTheme = [[ThemeManager standardThemeManager] currentTheme];
        self.searchTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:NSLocalizedString(localizationKey, @"") attributes:@{NSForegroundColorAttributeName : currentTheme.colorSearchBarText}];
    }
    
    @end
    

提交回复
热议问题