简体   繁体   中英

Change the font size and font style of UISearchBar iOS 7

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

UITextField *textField = [[searchBar subviews] objectAtIndex:1];
[textField setFont:[UIFont fontWithName:@"Helvetica" size:20]];

Working in iOS 6 but it's getting crash in iOS 7

Try this, It's Working Fine for iOS 5.0 and up: (iOS 7 also)

- (void)viewDidLoad
{

[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setFont:[UIFont fontWithName:@"Helvetica" size:20]];

}

For iOS 8

- (void)viewDidLoad
    {

[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setDefaultTextAttributes:@{
            NSFontAttributeName: [UIFont fontWithName:@"Helvetica" size:20],
      }];

    }

The accepted answer did not work for me on iOS 7.1. I had to change it to this:

[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setDefaultTextAttributes:@{
            NSFontAttributeName: [UIFont fontWithName:@"Avenir-Heavy" size:20.],
}];

iOS 10 Swift 3:

UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSFontAttributeName:UIFont(name: "Avenir-Heavy", size: 22)!]

appearanceWhenContainedIn:在iOS9中已弃用,请使用以下命令:

[[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setDefaultTextAttributes:@{ NSFontAttributeName: [UIFont fontWithName:@"FontName" size:14]}];

As rmaddy said, you should not rely on the private subview structure of a standard UI component. That stuff changes and makes your code break. You should use provided APIs to do this stuff.

I think much safe approach is :

[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setFont:[UIFont systemFontOfSize:14]];

or you can also use this sample code :

for(UIView *subView in searchBar.subviews) {
    if ([subView isKindOfClass:[UITextField class]]) {
        UITextField *searchField = (UITextField *)subView;
        searchField.font = [UIFont systemFontOfSize:14];
    }
}

The above code is also safer (at least compared to the one mentioned in the Question) as it's not using the index of subviews .

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<UIAppearanceContainer>)containerClass;
@end

// UIAppearance+Swift.m
@implementation UIView (UIViewAppearance_Swift)
+ (instancetype)my_appearanceWhenContainedIn:(Class<UIAppearanceContainer>)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)

For Swift 4 you need to reference the NSAttributedStringKey enum:

UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self])
        .defaultTextAttributes = [NSAttributedStringKey.font.rawValue: UIFont(...)]

In Swift 5

code of @Piotr Tomasik still working in swift 5 after bit changes

UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSAttributedString.Key.font:UIFont(name: "poppins", size: 13)!]
  • SWIFT
  • IOS 8

For me, this worked using a recursive function to find the actual textfield.

extension UIView {

func recursive_applyTheme_Search(
    #dynamicTextStyle: NSString,
    bgColor: UIColor,
    cursorColor: UIColor,
    textColor: UIColor,
    placeholderTextColor: UIColor,
    borderColor: UIColor,
    borderWidth: CGFloat,
    cornerRadius: CGFloat) {

    for subview in self.subviews
    {
        if subview is UITextField {

            (subview as! UITextField).applyThemeForSearchBar(
                dynamicTextStyle: dynamicTextStyle,
                bgColor: bgColor,
                cursorColor: cursorColor,
                textColor: textColor,
                placeholderTextColor: placeholderTextColor,
                borderColor: borderColor,
                borderWidth: borderWidth,
                cornerRadius: cornerRadius)
        }
        else { subview.recursive_applyTheme_Search(
                dynamicTextStyle: dynamicTextStyle,
                bgColor: bgColor,
                cursorColor: cursorColor,
                textColor: textColor,
                placeholderTextColor: placeholderTextColor,
                borderColor: borderColor,
                borderWidth: borderWidth,
                cornerRadius: cornerRadius) }
    }

}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM