占位符文本在UISearchBar的UITextField中未對齊

[英]Placeholder text misaligned in UITextField of UISearchBar


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?

我試圖通過外觀代理自定義UISearchBar的字體。它工作,但不知何故,占位符文本不垂直居中。我試着設置垂直對齊屬性;占位符文本將居中,但輸入的文本將略低於中心線...有沒有辦法解決這個問題?

Edit: Code I used to add custom font:

編輯:代碼我用來添加自定義字體:

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

placeholder text

normal text

3 个解决方案

#1


1  

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.

每當這種情況發生在我身上時,通常是由於UIAppearance的某些修改與UISearchBar混合不好造成的。看看你以前做過的任何黑客攻擊,或者嘗試一次一個地評論它們。

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.

在我的特定情況下,它實際上是對文本屬性中的NSForegroundColor的修改,因此這種不對齊可能與您的字體更改無關。

#2


0  

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.

您需要在searchBar中設置textField的對齊方式。從UISearch欄的子視圖中獲取textField,只需將其對齊設置為center。請嘗試下面的示例代碼。

- (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.

這里_searchbar是UISearchBar的出口。

#3


0  

- (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.

試試這段代碼一次。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/04/11/779ee34bdd1e5abc1bdb82c9c6d9281.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com