Change the font size of UISearchBar

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

How can I change the font size of UISearchBar ?

19条回答
  •  Happy的楠姐
    2020-12-04 15:53

    I wanted to use 'Roboto-Regular' as default font for all the uisearchbars in my project. I made an uisearchbar extension.

    extension UISearchBar {
    func addRobotoFontToSearchBar(targetSearchBar:UISearchBar?) -> UISearchBar
    {
        let textFieldInsideSearchBar = targetSearchBar!.valueForKey("searchField") as! UITextField
        textFieldInsideSearchBar.font = UIFont(name: "Roboto-Regular", size: 15.0)
    
    //UIBarButtons font in searchbar
    let uiBarButtonAttributes: NSDictionary = [NSFontAttributeName: UIFont(name: "Roboto-Regular", size: 15.0)!] 
    UIBarButtonItem.appearance().setTitleTextAttributes(uiBarButtonAttributes as? [String : AnyObject], forState: UIControlState.Normal)
    
        return targetSearchBar!
    }
    }
    

    Usage:

    self.sampleSearchBar.addRobotoFontToSearchBar(sampleSearchBar)
    

提交回复
热议问题