更改UISearchBar的字体大小 [英] Change the font size of UISearchBar

查看:149
本文介绍了更改UISearchBar的字体大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何更改 UISearchBar 的字体大小?

修改

答案


Answer

for(int i =0; i<[searchbar.subviews count]; i++) {
        if([[searchbar.subviews objectAtIndex:i] isKindOfClass:[UITextField class]])
            [(UITextField*)[searchbar.subviews objectAtIndex:i] setFont:[UIFont fontWithName:@"Helvetica" size:12]];
    }

谢谢 -
Pankaj

Thanks
Pankaj

推荐答案

UISearchBar里面有一个UITextField,但没有属性可以访问它。因此,没有办法以标准方式进行。

The UISearchBar has a UITextField inside, but there's no property to access it. So, there is no way for doing it in a standard way.

但是有一种非标准的解决方法。
UISearchBar继承自UIView,你可以使用[searchBar subviews]访问它的子视图。
如果您在控制台中打印它的子视图,您将看到它有这些视图。

But there is a non-stardard way to workaround it. UISearchBar inherits from UIView, you can access it's subviews using [searchBar subviews]. If you print in the console it's subviews you will see that it have these views.

UISearchBarBackground,UISearchBarTextField。

UISearchBarBackground, UISearchBarTextField.

因此,要更改字体大小,您只需要这样做

So, to change the font size you will only need to do that

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

但是,如果UISearchBar将来发生变化且文本字段不再位于第1位,你的程序会崩溃,所以最好通过子视图检查UITextField的位置,然后设置字体大小。

But, if the UISearchBar changes in the future and the textfield isn't in the position 1 anymore, your program will crash, so it's better to check through the subviews the position of the UITextField and then set the font size.

这篇关于更改UISearchBar的字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆