UISearchBar change placeholder color

前端 未结 17 2412
南旧
南旧 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 16:05
    if let textFieldInsideSearchBar = searchBar.value(forKey: "searchField") as ? UITextField {
        textFieldInsideSearchBar ? .textColor = UIColor.white
    
        if let textFieldInsideSearchBarLabel = textFieldInsideSearchBar!.value(forKey: "placeholderLabel") as ? UILabel {
            textFieldInsideSearchBarLabel ? .textColor = UIColor.white
    
            if let clearButton = textFieldInsideSearchBar ? .value(forKey: "clearButton") as!UIButton {
    
                clearButton.setImage(clearButton.imageView ? .image ? .withRenderingMode(.alwaysTemplate),
                    for : .normal)
                clearButton.tintColor = UIColor.white
            }
        }
    
        let glassIconView = textFieldInsideSearchBar ? .leftView as ? UIImageView
    
        glassIconView ? .image = glassIconView ? .image ? .withRenderingMode(.alwaysTemplate)
        glassIconView ? .tintColor = UIColor.white
    }
    
    0 讨论(0)
  • 2020-12-04 16:06

    Try this:

    [self.searchBar setValue:[UIColor whatever] forKeyPath:@"_searchField._placeholderLabel.textColor"];
    

    You can also set this in storyboard, select search bar, add entry under User Defined Runtime Attributes:

    _searchField._placeholderLabel.textColor
    

    of type Color and select the color you need.

    0 讨论(0)
  • 2020-12-04 16:08

    First solution is OK, but if you use multiple UISearchBar, or create a lot of instances it may fail. The one solution that always work for me is to use also appearance proxy but directly on UITextField

       NSDictionary *placeholderAttributes = @{
                                                NSForegroundColorAttributeName: [UIColor darkButtonColor],
                                                NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:15],
                                                };
    
        NSAttributedString *attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.searchBar.placeholder
                                                                                    attributes:placeholderAttributes];
    
        [[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setAttributedPlaceholder:attributedPlaceholder];
    
    0 讨论(0)
  • 2020-12-04 16:10

    It is easy from iOS 13.0 onwards, You can simply use searchTextField property of a search bar to update attributed properties of the placeholder.

    self.searchController.searchBar.searchTextField.attributedPlaceholder =  NSAttributedString.init(string: "Search anything...", attributes: [NSAttributedString.Key.foregroundColor:UIColor.red])
    

    One line solution

    0 讨论(0)
  • 2020-12-04 16:13

    Here is a Solution for Swift:

    Swift 2

    var textFieldInsideSearchBar = searchBar.valueForKey("searchField") as? UITextField
    textFieldInsideSearchBar?.textColor = UIColor.whiteColor()
    
    var textFieldInsideSearchBarLabel = textFieldInsideSearchBar!.valueForKey("placeholderLabel") as? UILabel
    textFieldInsideSearchBarLabel?.textColor = UIColor.whiteColor()
    

    Swift 3

    let textFieldInsideSearchBar = searchBar.value(forKey: "searchField") as? UITextField
    textFieldInsideSearchBar?.textColor = UIColor.white
    
    let textFieldInsideSearchBarLabel = textFieldInsideSearchBar!.value(forKey: "placeholderLabel") as? UILabel
    textFieldInsideSearchBarLabel?.textColor = UIColor.white
    
    0 讨论(0)
提交回复
热议问题