简体   繁体   English

更改以编程方式创建的UISearchBar的字体

[英]Change font for a programmatically created UISearchBar

I have found that the code in my AppDelegate : 我发现我的AppDelegate中的代码:

[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil]
 setFont:[UIFont systemFontOfSize:14]];

works to change the font of a UISearchBar created from IB. 用于更改从IB创建的UISearchBar的字体。 No problems there. 那里没有问题。

However, if I create the UISearchBar in code as such: 但是,如果我使用如下代码创建UISearchBar

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. 然后上面的UITextField外观代码无效。 Is there a way to change the font of a programmatically created UISearchBar ? 有没有办法更改以编程方式创建的UISearchBar的字体?

Thank you! 谢谢!

尝试一下,它在iOS 5.0及更高版本上运行良好:(也适用于iOS 7)

[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setFont:[UIFont fontWithName:@"League Gothic" size:20]];

You can try this 你可以试试这个

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

for more reference check this 有关更多参考,请检查

You can use this 你可以用这个

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

Hope this will help. 希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM