SWT
SWT

Reputation: 531

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

Upvotes: 25

Views: 15879

Answers (9)

Sanjay Mishra
Sanjay Mishra

Reputation: 702

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)!]

Upvotes: 1

Andrew
Andrew

Reputation: 1289

For Swift 4 you need to reference the NSAttributedStringKey enum:

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

Upvotes: 1

Piotr Tomasik
Piotr Tomasik

Reputation: 9194

iOS 10 Swift 3:

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

Upvotes: 6

Filip
Filip

Reputation: 33

appearanceWhenContainedIn: is deprecated in iOS9, use the following:

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

Upvotes: 2

Andrei Popa
Andrei Popa

Reputation: 169

  • 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) }
    }

}
}

Upvotes: 0

Mike Sprague
Mike Sprague

Reputation: 3637

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)

Upvotes: 1

Siva
Siva

Reputation: 1858

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],
      }];

    }

Upvotes: 67

joel.d
joel.d

Reputation: 1631

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.],
}];

Upvotes: 34

Bhavin
Bhavin

Reputation: 27225

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.

Upvotes: 1

Related Questions