Change the font size of UISearchBar

前端 未结 19 1306
情歌与酒
情歌与酒 2020-12-04 15:08

How can I change the font size of UISearchBar ?

相关标签:
19条回答
  • 2020-12-04 16:02

    the full code should be below:

    for (UIView *v in (SYSTEM_VERSION_LESS_THAN(@"7.0")?searchBar.subviews:[[searchBar.subviews objectAtIndex:0] subviews])) {
    
        if([v isKindOfClass:[UITextField class]]) {
            UITextField *textField = (UITextField *)v;
            [textField setFont:fREGULAR(@"15.0")];
    
            return;
        }
    }
    
    0 讨论(0)
  • 2020-12-04 16:03

    I know this is an old thread, but since iOS 13 UISearchBar has the searchTextField property on which you can set your desired font.

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

    In swift 2.0 for a search bar created programatically you could do this:

    override func layoutSubviews() {
      super.layoutSubviews()
    
      let textFieldInsideSearchBar = self.searchBar.valueForKey("searchField") as! UITextField
      textFieldInsideSearchBar.font = UIFont.systemFontOfSize(20)
     }
    

    In this case I put the code in a UIView subclass but it will work in other places too (i.e. viewWillAppear from the UIViewController)

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

    In swift 3 , you can achieve this by :

    UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).font = UIFont(name: "AvenirNextCondensed-Regular", size: 17.0)
    
    0 讨论(0)
  • 2020-12-04 16:05

    Rewrote Eugene's recursive solution in swift - changed the function name to more accurately describe function. For my case, I needed to change the font size. This worked for me.

    func findAndSetTextField(view: UIView) {
        if (view.subviews.count == 0){
            return
        }
    
        for var i = 0; i < view.subviews.count; i++ {
            var subview : UIView = view.subviews[i] as UIView
            if (subview.isKindOfClass(UITextField)){
                var searchField : UITextField = subview as UITextField
                searchField.font = UIFont(name: "Helvetica", size: 19.0)!
             }
             findAndSetTextField(subview)
        }
    }
    
    0 讨论(0)
  • 2020-12-04 16:07

    iOS 13+

      searchBar.searchTextField.font = UIFont(name: "YOUR-FONT-NAME", size: 13)
    
    0 讨论(0)
提交回复
热议问题