Change the font size and font style of UISearchBar iOS 7

15,769

Solution 1

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

    }

Solution 2

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

Solution 3

iOS 10 Swift 3:

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

Solution 4

appearanceWhenContainedIn: is deprecated in iOS9, use the following:

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

Solution 5

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)!]
Share:
15,769

Related videos on Youtube

SWT
Author by

SWT

i'm a iPhone and iPad Application Developer...

Updated on June 26, 2022

Comments

  • SWT
    SWT about 2 years

    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

    • rmaddy
      rmaddy over 10 years
      What is the error? And never dig into the private subview structure of a standard UI component. That stuff changes and makes your code break. Use provided APIs to do this stuff.
    • o15a3d4l11s2
      o15a3d4l11s2 over 10 years
      Please check this: stackoverflow.com/questions/19048766/… I think what you try to achieve is the same.
    • Bhavin
      Bhavin over 10 years
      @rmaddy is correct and detailed explanation of safe approach is given in my Answer.
  • Manann Sseth
    Manann Sseth over 10 years
    Works like a charm..!
  • rickerbh
    rickerbh over 10 years
    The second approach here doesn't appear to be that safe. You're still relying on the subview structure, as you're assuming that a UISearchBar has a UITextField as as direct subview. Just because you're avoiding an index doesn't mean you're avoiding the private subview structure.
  • Bhavin
    Bhavin over 10 years
    @rickerbh: Yeah, true. Actually, even I believe that OP should use the first approach that I mentioned in my Answer.
  • Ganesh
    Ganesh almost 10 years
    I have small query on this. I have implemented the same and it is working.But if I navigate to some view controller and back to the same screen the font is changing to default.Any Idea?
  • Mike
    Mike almost 10 years
    Same thing here on iOS 8. Works fine the first time. But when I navigate to another screen and back, the font is back to the system default.
  • Mike
    Mike almost 10 years
    This way overcomes the issue [tried on iOS 8.0] mentioned in Ganesh's comment (that by only using the setFont and not setDefaultTextAttribute the text is only changed on the first view and not after navigating from and back to the screen in questions).
  • Harshit Gupta
    Harshit Gupta over 9 years
    this answer is better.
  • Mike Gledhill
    Mike Gledhill over 9 years
    Yup, this was the only method which would work each time for me, on iOS 8.1.
  • Andrei Popa
    Andrei Popa about 9 years
    Also, most of WWDC videos recommend that font should conform to Dynamic Type, use let font = UIFont.preferredFontForTextStyle(UIFontTextStyleBody)
  • jeet.chanchawat
    jeet.chanchawat over 8 years
  • Adam Kaplan
    Adam Kaplan over 8 years
    @jeet.chanchawat – that does not matter. The appearance is defined for UITextFields that are subviews of UISearchBar, and there is one in there.
  • Rishi
    Rishi over 6 years
    Works as expedited in iOS11(Swift 4). Especially .rawValue is what I was missing.
  • Sathe_Nagaraja
    Sathe_Nagaraja over 6 years
    'appearanceWhenContainedIn:' is deprecated: first deprecated in iOS 9.0 - Use +appearanceWhenContainedInInstancesOfClasses: instead [[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setDefaultTextAttributes:@{ NSFontAttributeName: [UIFont fontWithName:@"Helvetica" size:20] }];
  • Erick Maynard
    Erick Maynard almost 6 years
    Type 'NSAttributedStringKey' (aka 'NSString') has no member 'font'
  • Konstantin Berkov
    Konstantin Berkov almost 6 years
    let appearence = UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]) This doesn't work: appearence.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.heavy) But this works fine: appearence.defaultTextAttributes = [NSAttributedStringKey.font.rawValue: UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.heavy)]