【问题标题】:UISearchBar change placeholder colorUISearchBar 更改占位符颜色
【发布时间】:2012-08-03 09:11:31
【问题描述】:

关于如何更改 UISearchBar 的占位符文本的文本颜色,有人有任何想法或代码示例吗?

【问题讨论】:

  • 我认为这篇文章会对你有所帮助stackoverflow.com/questions/1340224/…
  • 我已经看过了。这篇文章涉及一个 UITextfield。子类化 UITextfield 并覆盖 -drawPlaceholderInRect 方法肯定会有所帮助。但是,UISearchBar 不是 UITextfield 的子类,因此我无法重写此方法。
  • 对不起,我不知道这个。 UISearchBar 是 UIView 的子类,我认为您可以从中继承并尝试覆盖 drawRect 方法,并添加上一篇文章中的代码。也许这有帮助
  • 对于 Swift 3。我在这里找到了解决方案:Customize textfield easily

标签: ios uisearchbar placeholder


【解决方案1】:

对于iOS5+,使用外观代理

[[UILabel appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor redColor]];

【讨论】:

  • 自从我的帖子我找到了答案,这确实是最好的方法。干净,简单,不会破坏任何东西。
  • 这不是真的有效。 UILabel 不支持这种方式的自定义,因为 UILabel 的textColor 属性没有按照文档的要求用UI_APPEARANCE_SELECTOR 标记。另见stackoverflow.com/questions/11839044/…
  • 只有当我在 Stroyboard 中添加一些占位符时,这才对我有用。如果我们在情节提要中没有占位符,直接通过代码设置占位符是行不通的。有线
  • 我偶然给了这个东西一个赞成票。它不好用。人们应该使用:stackoverflow.com/questions/1340224/…
  • 这不是一个有效的答案,请看下面我的答案
【解决方案2】:

Change UITextField's placeholder text color programmatically找到答案

// Get the instance of the UITextField of the search bar
UITextField *searchField = [searchBar valueForKey:@"_searchField"];

// Change search bar text color
searchField.textColor = [UIColor redColor];

// Change the search bar placeholder text color
[searchField setValue:[UIColor blueColor] forKeyPath:@"_placeholderLabel.textColor"];

【讨论】:

  • 有人尝试使用该代码提交应用程序吗?应该不是什么大问题……但谁能肯定地说……
  • 虽然此代码有效,但我会相信任何提交的应用程序,因为它违反了 Apple 的规定。
  • 我的应用可以通过这个解决方案提交到应用商店。 :)
  • 我担心这会随着 iOS 更新而突然崩溃。
  • prompt 参数呢??
【解决方案3】:

第一个解决方案是可以的,但是如果你使用多个 UISearchBar,或者创建很多实例它可能会失败。始终对我有用的一种解决方案是也使用外观代理,但直接在 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];

【讨论】:

  • 这是唯一适合我的解决方案,其他解决方案在来回导航 viewController 层次结构以及破坏/重新创建搜索栏时失败
  • 这应该是 ObjC/iOS 13 接受的解决方案
【解决方案4】:

这是 Swift 的解决方案:

斯威夫特 2

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

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

斯威夫特 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

【讨论】:

  • 我想是的。它与其他用户推荐的解决方案相同。
  • prompt 怎么样?! (不是占位符)
  • 尝试为 textFieldInsideSearchBar 设置 tintcolor - 应该可以正常工作!
  • 适用于 iOS 10.2.1
  • 效果很好,但您可以链接到本主题中的苹果文档。我很难找到它。提前致谢。
【解决方案5】:

试试这个看看:(我用 Swift 4.1 测试了下面的代码 - Xcode 9.3-beta4

@IBOutlet weak var sbSearchBar: UISearchBar!

if let textfield = sbSearchBar.value(forKey: "searchField") as? UITextField {

    textfield.backgroundColor = UIColor.yellow
    textfield.attributedPlaceholder = NSAttributedString(string: textfield.placeholder ?? "", attributes: [NSAttributedStringKey.foregroundColor : UIColor.red])

    textfield.textColor = UIColor.green

    if let leftView = textfield.leftView as? UIImageView {
        leftView.image = leftView.image?.withRenderingMode(.alwaysTemplate)
        leftView.tintColor = UIColor.red
    }
}

结果如下:

【讨论】:

    【解决方案6】:
    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
    }
    

    【讨论】:

      【解决方案7】:

      Swift 5 - ios 13

      那些被卡住的人让我告诉你它只能在 viewDidLayoutSubviews 中工作,而不是在 viewDidLoad 中

      override func viewDidLayoutSubviews() {
      
          setupSearchBar(searchBar: YourSearchBar)
      
      }
      
          func setupSearchBar(searchBar : UISearchBar) {
      
          searchBar.setPlaceholderTextColorTo(color: UIColor.white)        
      
         }
      
      extension UISearchBar
      {
          func setPlaceholderTextColorTo(color: UIColor)
          {
              let textFieldInsideSearchBar = self.value(forKey: "searchField") as? UITextField
              textFieldInsideSearchBar?.textColor = color
              let textFieldInsideSearchBarLabel = textFieldInsideSearchBar!.value(forKey: "placeholderLabel") as? UILabel
              textFieldInsideSearchBarLabel?.textColor = color
          }
      }
      

      编码愉快:)

      【讨论】:

        【解决方案8】:

        看起来 Apple 不希望我们在 UISearchBar 类中使用占位符颜色。所以,让我们创建自己的占位符标签!

        • 没有子类化。
        • 适用于 iOS 13 SDK。
        • 只是一种简单的解决方法。

        let searchBar = searchController.searchBar
        // ...
        // Configure `searchBar` if needed
        // ...
        
        let searchTextField: UITextField
        if #available(iOS 13, *) {
            searchTextField = searchBar.searchTextField
        } else {
            searchTextField = (searchBar.value(forKey: "searchField") as? UITextField) ?? UITextField()
        }
        
        // Since iOS 13 SDK, there is no accessor to get the placeholder label.
        // This is the only workaround that might cause issued during the review.
        if let systemPlaceholderLabel = searchTextField.value(forKey: "placeholderLabel") as? UILabel {
            // Empty or `nil` strings cause placeholder label to be hidden by the search bar
            searchBar.placeholder = " "
        
            // Create our own placeholder label
            let placeholderLabel = UILabel(frame: .zero)
        
            placeholderLabel.text = "Search"
            placeholderLabel.font = UIFont.systemFont(ofSize: 17.0, weight: .regular)
            placeholderLabel.textColor = UIColor.blue.withAlphaComponent(0.5)
        
            systemPlaceholderLabel.addSubview(placeholderLabel)
        
            // Layout label to be a "new" placeholder
            placeholderLabel.leadingAnchor.constraint(equalTo: systemPlaceholderLabel.leadingAnchor).isActive = true
            placeholderLabel.topAnchor.constraint(equalTo: systemPlaceholderLabel.topAnchor).isActive = true
            placeholderLabel.bottomAnchor.constraint(equalTo: systemPlaceholderLabel.bottomAnchor).isActive = true
            placeholderLabel.translatesAutoresizingMaskIntoConstraints = false
            placeholderLabel.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
        } else {
            searchBar.placeholder = ""
        }
        

        【讨论】:

          【解决方案9】:

          斯威夫特 3

          UILabel.appearance(whenContainedInInstancesOf: [UISearchBar.self]).textColor = UIColor.white
          

          【讨论】:

            【解决方案10】:

            iOS 13

            之前的解决方案可能无法在 iOS 13 上运行,因为添加了新的 searchTextField,您可以在其上设置属性字符串。

            我把它归入类别:

            @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
            

            【讨论】:

            • 我曾多次尝试分配给attributedPlaceholder,但没有成功。它接受字符串内容,但不更改文本颜色。搜索栏位于导航项内。
            【解决方案11】:

            从 iOS 13.0 开始很容易,您可以简单地使用搜索栏的 searchTextField 属性来更新占位符的属性属性。

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

            一线解决方案

            【讨论】:

              【解决方案12】:

              iOS 13

              使用自定义搜索栏子类。

              这也适用于 UISearchControllerUINavigationItem 中的一部分(带有 hidesSearchBarWhenScrolling = true)。

              我们希望在应用UIAppearance 代理后立即应用我们的更改,因为这些是最可能的根本原因:

              class MySearchBar : UISearchBar {
                  // Appearance proxies are applied when a view is added to a view hierarchy, so apply your tweaks after that:
                  override func didMoveToSuperview() {
                      super.didMoveToSuperview() // important! - system colors will not apply correctly on ios 11-12 without this
              
                      let placeholderColor = UIColor.white.withAlphaComponent(0.75)
                      let placeholderAttributes = [NSAttributedString.Key.foregroundColor : placeholderColor]
                      let attributedPlaceholder = NSAttributedString(string: "My custom placeholder", attributes: placeholderAttributes)
                      self.searchTextField.attributedPlaceholder = attributedPlaceholder
                      
                      // Make the magnifying glass the same color
                      (self.searchTextField.leftView as? UIImageView)?.tintColor = placeholderColor
                  }
              }
              
              // Override `searchBar` as per the documentation
              private class MySearchController : UISearchController {
                  private lazy var customSearchBar = MySearchBar()
                  override var searchBar: UISearchBar { customSearchBar }
              }
              

              这需要相当长的时间才能正常工作......

              【讨论】:

                【解决方案13】:

                试试这个:

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

                你也可以在storyboard中设置,选择搜索栏,在User Defined Runtime Attributes下添加条目:

                _searchField._placeholderLabel.textColor
                

                类型颜色并选择您需要的颜色。

                【讨论】:

                  【解决方案14】:

                  IOS 13

                  上为我工作
                  searchBar.searchTextField.attributedPlaceholder = NSAttributedString(
                      string: "Search something blabla",
                      attributes: [.foregroundColor: UIColor.red]
                  )
                  

                  【讨论】:

                    【解决方案15】:

                    在调查了几个答案后,我得出了这个,希望对它有所帮助

                    for (UIView *subview in searchBar.subviews) {
                        for (UIView *sv in subview.subviews) {
                            if ([NSStringFromClass([sv class]) isEqualToString:@"UISearchBarTextField"]) {
                    
                                if ([sv respondsToSelector:@selector(setAttributedPlaceholder:)]) {
                                    ((UITextField *)sv).attributedPlaceholder = [[NSAttributedString alloc] initWithString:searchBar.placeholder attributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}];
                                }
                                break;
                            }
                        }
                    }
                    

                    【讨论】:

                    • 我继承了 UISearchBar 类,然后像你一样使用了属性占位符。但是在我的子类中,我添加了 override var placeholder: String? { didSet { changePlaceholderColor(str: placeholder ?? "") } },这样,每当我更改此 searchBar 上的占位符文本时,它都会自动使占位符文本正确的颜色。如果我不这样做,那么如果我设置了一个普通的占位符,它将覆盖我的占位符颜色。
                    【解决方案16】:

                    此解决方案适用于 Xcode 8.2.1。使用 Swift 3.0。 :

                    extension UISearchBar
                    {
                        func setPlaceholderTextColorTo(color: UIColor)
                        {
                            let textFieldInsideSearchBar = self.value(forKey: "searchField") as? UITextField
                            textFieldInsideSearchBar?.textColor = color
                            let textFieldInsideSearchBarLabel = textFieldInsideSearchBar!.value(forKey: "placeholderLabel") as? UILabel
                            textFieldInsideSearchBarLabel?.textColor = color
                        }
                    }
                    

                    使用示例:

                    searchController.searchBar.setPlaceholderTextColorTo(color: mainColor)
                    

                    【讨论】:

                      【解决方案17】:

                      这是一个老问题,但对于现在遇到这个问题的人来说,您可以使用以下命令更改 iOS 8.x - 10.3 上的搜索图标:

                      [_searchBar setImage:[UIImage imageNamed:@"your-image-name"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
                      

                      关于占位符文本颜色,您可以在此处查看我的其他答案,该答案使用类别:UISearchBar change placeholder color

                      【讨论】:

                        【解决方案18】:

                        试试这个:

                          UITextField *searchField = [searchbar valueForKey:@"_searchField"];
                          field.textColor = [UIColor redColor]; //You can put any color here.
                        

                        【讨论】:

                        • 我不想改变普通文本的颜色。我想更改占位符文本的颜色。
                        • 这不行!执行此操作时,您正在访问 PRIVATE iVAR,这可能会导致您的应用被应用商店拒绝。
                        • @AlexanderW :如果这不是要使用的,你将无法访问它......世界各地的每个人都使用这个东西来改变文本字段占位符颜色变化的颜色...... . 我的应用程序从未因此而被拒绝...但它只是一个解决方案,可能不是一个完美的解决方案.....
                        猜你喜欢
                        • 2015-06-05
                        • 1970-01-01
                        • 1970-01-01
                        • 2012-04-01
                        • 2021-09-28
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 2017-05-18
                        相关资源
                        最近更新 更多