kurisukun
kurisukun

Reputation: 3159

Change font for a programmatically created UISearchBar

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!

Upvotes: 0

Views: 3210

Answers (3)

Abuzar Amin
Abuzar Amin

Reputation: 1991

You can use this

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

Hope this will help.

Upvotes: 0

Siva
Siva

Reputation: 1858

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

Upvotes: 4

Toseef Khilji
Toseef Khilji

Reputation: 17409

You can try this

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

for more reference check this

Upvotes: 0

Related Questions