14

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

4

3 回答 3

1

每当这种情况发生在我身上时,通常是由于一些修改UIAppearance无法与UISearchBar. 查看您之前可能做过的任何黑客攻击,也许尝试一次将它们评论出来。

在我的特殊情况下,它实际上是对NSForegroundColor文本属性的修改,因此这种错位可能与您的字体更改无关。

于 2015-08-10T03:53:52.227 回答
0

您需要在 searchBar 内设置 textField 的对齐方式。从 UISearch 栏的子视图中获取 textField 并简单地将其对齐设置为中心。试试下面的示例代码。

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

这里 _searchbar 是 UISearchBar 的出口。

于 2013-04-11T04:14:53.743 回答
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];

}

试试这个代码一次。

于 2013-04-11T06:57:03.103 回答