Change the font size of UISearchBar

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

How can I change the font size of UISearchBar ?

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

    The accepted answer is not the recommended way of applying font for UISearchBar. Better to avoid this approach and use the answers provided by @rafalkitta or @josema.

    But be cautious, this will be applied to all the search bars through out the app. In order to apply this approach to a particular search bar: create a subclass of UISearchBar and set the defaultTextAttributes on the UITextField like below

    Swift4:

        let defaultTextAttribs = [NSAttributedStringKey.font.rawValue: UIFont.boldSystemFont(ofSize: 21.0), NSAttributedStringKey.foregroundColor.rawValue:UIColor.red]
        UITextField.appearance(whenContainedInInstancesOf: [CustomSearchBar.self]).defaultTextAttributes = defaultTextAttribs
    

    Obj-C(iOS11)

        UIFont *font = [UIFont boldSystemFontOfSize:21.0];
        UIColor *color = [UIColor redColor];
        NSDictionary * defaultTextAttribs = @{NSFontAttributeName:font, NSForegroundColorAttributeName: color};
        [UITextField appearanceWhenContainedInInstancesOfClasses:@[[CustomSearchBar class]]].defaultTextAttributes = defaultTextAttribs;
    
    0 讨论(0)
提交回复
热议问题