Change the font size and font style of UISearchBar iOS 7

后端 未结 9 835
借酒劲吻你
借酒劲吻你 2020-12-13 03:17

How can I change the font size and font style of UISearchBar in iOS 7?

UITextField *textField = [[searchBar subviews] objectAtIndex:1];
[textField setFont:[U         


        
9条回答
  •  不思量自难忘°
    2020-12-13 03:28

    For those looking for a working Swift version, I've adapted this answer. Swift doesn't currently support Objc varargs methods, so it doesn't work directly with the above methods. We can work around this by making an objective-c category that doesn't use varargs and calls what we need:

    // UIAppearance+Swift.h
    @interface UIView (UIViewAppearance_Swift)
    // appearanceWhenContainedIn: is not available in Swift. This fixes that.
    + (instancetype)my_appearanceWhenContainedIn:(Class)containerClass;
    @end
    

    // UIAppearance+Swift.m
    @implementation UIView (UIViewAppearance_Swift)
    + (instancetype)my_appearanceWhenContainedIn:(Class)containerClass {
        return [self appearanceWhenContainedIn:containerClass, nil];
    }
    @end
    

    Just be sure to #import "UIAppearance+Swift.h" in your bridging header.

    Then just call:

    UITextField.my_appearanceWhenContainedIn(UISearchBar.self).font = UIFont.systemFontOfSize(14.0)
    

提交回复
热议问题