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
  • 10,924
  • 9
  • 51
  • 60
kurisukun
  • 3,109
  • 5
  • 35
  • 68
  • 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,838
  • 2
  • 22
  • 37
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,042
  • 11
  • 81
  • 118
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,921
  • 1
  • 16
  • 32