Rushi trivedi
Rushi trivedi

Reputation: 747

Custom font in UISearchBar

i have installed avenir font in x-code for custom font.

and i have tried this code to set custom font to search bar text

 UITextField *textField = [[sbOffers subviews] objectAtIndex:1];

 [textField setFont:[UIFont fontWithName:@"AvenirNext-Medium" size:25]];

but this is not worked for me i got error.Like this

enter image description here

is there any solution for this or anyother way to set font for UISearchBar ?? thanks in advance

Upvotes: 0

Views: 510

Answers (1)

Seryozha
Seryozha

Reputation: 1639

Well, I believe your code works fine up to iOS 7. Since iOS 7, there are some changes in UISearchBar subviews hierarchy. Here is correct way to get UITextField inside UISearchBar,

for (UIView *view in [[yourSearchBar.subviews lastObject] subviews]) {
    if ([view isKindOfClass:[UITextField class]]) {
        [(UITextField *)view setFont:[UIFont fontWithName:@"AvenirNext-Medium" size:25]];
    }
}

Upvotes: 1

Related Questions