0

I have found that the code in my AppDelegate:

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

works to change the font of a UISearchBar created from IB. No problems there.

However, if I create the UISearchBar in code as such:

UISearchBar *bar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
bar.placeholder = @"test";
[self.view addSubview:bar];

Then the above UITextField appearance code has no effect. Is there a way to change the font of a programmatically created UISearchBar?

Thank you!

Axel Guilmin
  • 11,454
  • 9
  • 54
  • 64
kurisukun
  • 3,149
  • 5
  • 37
  • 69
  • 1
    I guess this answer will work, http://stackoverflow.com/questions/4697689/change-the-font-size-of-uisearchbar – arthankamal Aug 06 '13 at 03:07

3 Answers3

4

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

[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setFont:[UIFont fontWithName:@"League Gothic" size:20]];
Siva
  • 1,848
  • 2
  • 23
  • 40
0

You can try this

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

for more reference check this

Community
  • 1
  • 1
Toseef Khilji
  • 17,192
  • 12
  • 80
  • 121
0

You can use this

UITextField *textField = [self.searchBar valueForKey: @"_searchField"];
[textField setFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:17.0]];

Hope this will help.

Abuzar Amin
  • 1,981
  • 1
  • 19
  • 33