14
votes

I'm trying to customize UISearchBar's font through appearance proxy. It works, but somehow, the placeholder text is not centered vertically. I tried to set the vertical alignment property; the placeholder text would get centred but text entered would be slightly below the center line... Is there any way to fix this?

Edit: Code I used to add custom font:

[[UITextField appearanceWhenContainedIn: [UISearchBar class], nil]
                                setFont: [UIFont customFontWithSize: 17]];

placeholder text

normal text

3
how are you setting the placeholder???Kasaname
show the code you're using to setup the appearance and the placeholderGabriele Petronella
@GabrielePetronella editedSChang
searchBar.placeholder = @"ENTER AN ADDRESS AND AS YOU WANT"; use withiPatel

3 Answers

1
votes

Whenever this happens to me, it is usually caused by some modification to UIAppearance not blending well with UISearchBar. Look at any previous hacks you may have made and perhaps try commenting them out one at a time.

In my particular case it was actually a modification to NSForegroundColor in a text attribute, so this misalignment may be unrelated to your font change.

0
votes

You need to set alignment of textField inside the searchBar. Get textField from subviews of UISearch bar and simply set its alignment to center.Try sample code below.

- (void)viewDidLoad
{
    for (UITextField *txt in _searchbar.subviews) {
        if ([txt isKindOfClass:[UITextField class]]) {
            [txt setTextAlignment:NSTextAlignmentCenter];
        }
    }
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

here _searchbar is outlet of UISearchBar.

0
votes
- (void)viewDidLoad
{

    for (UITextField *txt in search.subviews) {
        if ([txt isKindOfClass:[UITextField class]]) {
            txt.font = [UIFont fontWithName:@"Arial-BoldMT" size:17];
            txt.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
        }
    }

    [super viewDidLoad];

}

Try this code once.