Ios 更改UISearchBar的字体大小
如何更改Ios 更改UISearchBar的字体大小,ios,objective-c,uisearchbar,uifont,Ios,Objective C,Uisearchbar,Uifont,如何更改UISearchBar的字体大小?UISearchBar内部有一个UITextField,但没有可访问的属性。所以,没有办法用标准的方式来做 但有一种非标准的解决方法。 UISearchBar继承自UIView,您可以使用[searchBar SubView]访问它的子视图。 如果您在控制台中打印它的子视图,您将看到它有这些视图 UISearchBarBackground,UISearchBarTextField 因此,要更改字体大小,您只需这样做 UITextField *te
UISearchBar
的字体大小?UISearchBar内部有一个UITextField,但没有可访问的属性。所以,没有办法用标准的方式来做
但有一种非标准的解决方法。
UISearchBar继承自UIView,您可以使用[searchBar SubView]访问它的子视图。
如果您在控制台中打印它的子视图,您将看到它有这些视图
UISearchBarBackground,UISearchBarTextField
因此,要更改字体大小,您只需这样做
UITextField *textField = [[searchBar subviews] objectAtIndex:1];
[textField setFont:[UIFont fontWithName:@"Helvetica" size:40]];
但是,如果UISearchBar将来更改,并且textfield不再位于位置1,您的程序将崩溃,因此最好通过子视图检查UITextField的位置,然后设置字体大小。执行此操作的安全方法如下:
for(UIView *subView in searchBar.subviews) {
if ([subView isKindOfClass:[UITextField class]]) {
UITextField *searchField = (UITextField *)subView;
searchField.font = [UIFont fontWithName:@"Oswald" size:11];
}
}
为什么这比公认的答案更安全?因为它不依赖于UITextField的索引保持不变。(这也是一个更干净的循环)我建议iOS 5.0及更高版本使用不同的选项:
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setFont:[UIFont systemFontOfSize:14]];
对于iOS 8(由Mike Gledhill链接):
对于iOS 9及以上版本:
[[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setDefaultTextAttributes:@{NSFontAttributeName: [UIFont fontWithName:@"Helvetica" size:20]}];
这样,您就不必为应用程序中的每个搜索栏枚举子视图了。请尝试按其键查找搜索栏:
UITextField *searchField = [self.searchDisplayController.searchBar valueForKey:@"_searchField"];
searchField.font = [[UIFont fontWithName:@"Oswald" size:11];
完整代码应如下所示:
for (UIView *v in (SYSTEM_VERSION_LESS_THAN(@"7.0")?searchBar.subviews:[[searchBar.subviews objectAtIndex:0] subviews])) {
if([v isKindOfClass:[UITextField class]]) {
UITextField *textField = (UITextField *)v;
[textField setFont:fREGULAR(@"15.0")];
return;
}
}
UITextField
外观不适用于以编程方式创建的UISearchBar
s。您必须使用递归解决方法
- (void)viewDidLoad {
[super viewDidLoad];
[self recursivelySkinTextField:self.searchBar];
}
- (void)recursivelySkinTextField:(UIView *)view {
if (!view.subviews.count) return;
for (UIView *subview in view.subviews) {
if ([subview isKindOfClass:[UITextField class]]) {
UITextField *searchField = (UITextField *)subview;
searchField.font = [[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] font];
searchField.textColor = [[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] textColor];
}
[self recursivelySkinTextField:subview];
}
}
在swift中重写Eugene的递归解决方案-更改函数名称以更准确地描述函数。对于我的情况,我需要更改字体大小。这对我有用
func findAndSetTextField(view: UIView) {
if (view.subviews.count == 0){
return
}
for var i = 0; i < view.subviews.count; i++ {
var subview : UIView = view.subviews[i] as UIView
if (subview.isKindOfClass(UITextField)){
var searchField : UITextField = subview as UITextField
searchField.font = UIFont(name: "Helvetica", size: 19.0)!
}
findAndSetTextField(subview)
}
}
func findAndSetTextField(视图:UIView){
if(view.subviews.count==0){
返回
}
对于变量i=0;i
您可以使用KVC(键值编码)获取文本字段
UITextField *textField = [self.searchBar valueForKey: @"_searchField"];
[textField setTextColor:[UIColor redColor]];
[textField setFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:17.0]];
在swift 2.0中,对于通过编程创建的搜索栏,您可以执行以下操作:
override func layoutSubviews() {
super.layoutSubviews()
let textFieldInsideSearchBar = self.searchBar.valueForKey("searchField") as! UITextField
textFieldInsideSearchBar.font = UIFont.systemFontOfSize(20)
}
在本例中,我将代码放在UIView子类中,但它也可以在其他地方使用(即,ViewwillbeloundfromUIViewController)我想使用“Roboto Regular”作为项目中所有UISearchBar的默认字体。我做了一个uisearchbar扩展
extension UISearchBar {
func addRobotoFontToSearchBar(targetSearchBar:UISearchBar?) -> UISearchBar
{
let textFieldInsideSearchBar = targetSearchBar!.valueForKey("searchField") as! UITextField
textFieldInsideSearchBar.font = UIFont(name: "Roboto-Regular", size: 15.0)
//UIBarButtons font in searchbar
let uiBarButtonAttributes: NSDictionary = [NSFontAttributeName: UIFont(name: "Roboto-Regular", size: 15.0)!]
UIBarButtonItem.appearance().setTitleTextAttributes(uiBarButtonAttributes as? [String : AnyObject], forState: UIControlState.Normal)
return targetSearchBar!
}
}
用法:
self.sampleSearchBar.addRobotoFontToSearchBar(sampleSearchBar)
在swift 3中,您可以通过以下方式实现:
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).font = UIFont(name: "AvenirNextCondensed-Regular", size: 17.0)
您不应该像在其他答案中那样使用私有API。若你们想在整个应用程序中定制搜索栏,你们可以使用类的外观代理。它允许修改搜索栏文本属性、占位符属性、取消按钮、文本字段背景图像、背景图像等。 使用示例:
if #available(iOS 9.0, *) {
let searchBarTextAttributes = [
NSFontAttributeName: UIFont.boldSystemFont(ofSize: 12),
NSForegroundColorAttributeName: UIColor.red
]
// Default attributes for search text
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = searchBarTextAttributes
// Attributed placeholder string
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).attributedPlaceholder = NSAttributedString(string: "Search...", attributes: searchBarTextAttributes)
// Search bar cancel button
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(searchBarTextAttributes, for: .normal)
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).title = "Cancel"
}
接受的答案不是为UISearchBar应用字体的推荐方法。最好避免这种方法,使用@rafalkitta或@josema提供的答案 但要小心,这将适用于整个应用程序的所有搜索栏。为了将此方法应用于特定搜索栏:创建
UISearchBar
的子类,并在UITextField
上设置defaulttexttributes
,如下所示
Swift4:
let defaultTextAttribs = [NSAttributedStringKey.font.rawValue: UIFont.boldSystemFont(ofSize: 21.0), NSAttributedStringKey.foregroundColor.rawValue:UIColor.red]
UITextField.appearance(whenContainedInInstancesOf: [CustomSearchBar.self]).defaultTextAttributes = defaultTextAttribs
class MySearchBar: UISearchBar {
override func layoutSubviews() {
super.layoutSubviews()
if let textFieldInsideSearchBar = value(forKey: "searchField") as? UITextField {
textFieldInsideSearchBar.font = UIFont(name: "Oswald-Light", size: 14)
}
}
}
Obj-C(iOS11)
在定义或设置UISearchBar的地方添加以下代码 迅捷4:
UILabel.appearance(whenContainedInInstancesOf: [UISearchBar.self]).font = UIFont.init(name: "Ubuntu-Regular", size: 16)
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).font = UIFont.init(name: "Ubuntu-Regular", size: 16)
目标C:
[[UILabel appearanceWhenContainedIn:[UISearchBar class], nil] setFont:[UIFont fontWithName:@"Helvetica" size:12.0]];
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setFont:[UIFont fontWithName:@"Helvetica" size:12.0]];
适用于Swift 4.2+
let defaultTextAttributes = [
NSAttributedString.Key.font: UIFont.init(name: "Ubuntu-Regular", size: 16),
NSAttributedString.Key.foregroundColor: UIColor.gray
]
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = defaultTextAttributes
我知道这是一个旧线程,但由于iOS 13 UISearchBar具有searchTextField属性,您可以在该属性上设置所需字体。iOS 13+
searchBar.searchTextField.font = UIFont(name: "YOUR-FONT-NAME", size: 13)
如果您需要一种方便的方式来更改所有的应用程序搜索栏,那么在Swift 5中有一个简单的子类:
let defaultTextAttribs = [NSAttributedStringKey.font.rawValue: UIFont.boldSystemFont(ofSize: 21.0), NSAttributedStringKey.foregroundColor.rawValue:UIColor.red]
UITextField.appearance(whenContainedInInstancesOf: [CustomSearchBar.self]).defaultTextAttributes = defaultTextAttribs
class MySearchBar: UISearchBar {
override func layoutSubviews() {
super.layoutSubviews()
if let textFieldInsideSearchBar = value(forKey: "searchField") as? UITextField {
textFieldInsideSearchBar.font = UIFont(name: "Oswald-Light", size: 14)
}
}
}
iOS 13或更高版本:
if #available(iOS 13.0, *) {
// set your desired font size
self.searchBar.searchTextField.font = .systemFont(ofSize: 14.0)
}
谢谢,它工作得很好。。。我稍微编辑了你的代码,并将其作为EditDownVote发布在这里,因为这不是安全的推荐方式。更好的选择@josema.vitaminew answer此代码在将来的更新中可能中断的唯一方法是,如果搜索栏不再将
UITextField
作为子视图。但即使这样,它也不会崩溃。在iOS 8中不起作用,因为视图层次结构发生了变化。文本字段不再位于顶层。因为你的搜索不是递归的,它找不到文本字段。这是使用苹果API的正确答案!忽略此处建议的任何其他内容。这不起作用。为了使外观代理适用于特定的类/方法,类头文件需要该方法的UI\u外观\u选择器。如果打开UITextField,您会注意到它没有任何UI\u外观\u选择器注释。当我在声明UISearchBar并以编程方式将其添加到表视图后放置该行时,这对我来说是有效的。如果是事先,它什么也没做。我不确定如果你通过界面生成器添加搜索,它会如何工作。这似乎不适用于iOS 7+。我认为这是因为iOS 11+缺少的代理方法:-)只需设置UITextField.appearance(当包含instancesof:[UISearchBar.self])。defaulttexttributes=[…]
一般来说,这是最好的解决方案。只要内部textfield确实是UITextField
(或子类)并且在子视图层次结构中的某个位置,它就与版本无关。但我不明白你为什么要使用UIE。为什么不直接设置属性?@Dima我使用的是UIAppearance
,因为我首先通过外观代理设置字体和textColor值,但当您以编程方式创建搜索栏时,系统不会选择它,这就是为什么我使用此变通方法+外观值,因此,如果我决定在将来更改这些值,它们将在整个应用程序中更改。这可能会导致您的应用程序被拒绝,因为您正在访问私有财产。这个