ios 更改 UISearchBar 的字体大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4697689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change the font size of UISearchBar
提问by pankaj
How can I change the font size of UISearchBar
?
如何更改 的字体大小UISearchBar
?
采纳答案by Bruno Domingues
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 内部有一个 UITextField,但没有访问它的属性。所以,没有办法以标准的方式做到这一点。
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.
但是有一种非标准的方法来解决它。UISearchBar 继承自 UIView,您可以使用 [searchBar subviews] 访问它的子视图。如果您在控制台中打印它的子视图,您将看到它具有这些视图。
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]];
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 发生更改并且文本字段不再位于位置 1 时,您的程序将崩溃,因此最好通过子视图检查 UITextField 的位置,然后设置字体大小。
回答by José Manuel Sánchez
I suggest yet a different option for iOS 5.0 and up:
我建议为 iOS 5.0 及更高版本提供一个不同的选项:
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setFont:[UIFont systemFontOfSize:14]];
for iOS 8 (as linked by Mike Gledhill):
对于 iOS 8(由 Mike Gledhill 链接):
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setDefaultTextAttributes:@{
NSFontAttributeName: [UIFont fontWithName:@"Helvetica" size:20],
}];
for iOS 9 and above:
对于 iOS 9 及更高版本:
[[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setDefaultTextAttributes:@{NSFontAttributeName: [UIFont fontWithName:@"Helvetica" size:20]}];
This way you don't need to mess with enumerating subviews for every search bar in your app.
这样你就不需要为你的应用程序中的每个搜索栏枚举子视图。
回答by Gavin Miller
The safe way for performing this operation is as follows:
执行此操作的安全方法如下:
for(UIView *subView in searchBar.subviews) {
if ([subView isKindOfClass:[UITextField class]]) {
UITextField *searchField = (UITextField *)subView;
searchField.font = [UIFont fontWithName:@"Oswald" size:11];
}
}
Why is this safer than the accepted answer? Because it doesn't rely on the index of the UITextField staying constant. (it's also a cleaner for loop)
为什么这比接受的答案更安全?因为它不依赖于 UITextField 的索引保持不变。(它也是一个更清洁的 for 循环)
回答by Rishi
The accepted answer is not the recommended way of applying font for UISearchBar. Better to avoid this approach and use the answers provided by @rafalkitta or @josema.
接受的答案不是为 UISearchBar 应用字体的推荐方式。最好避免这种方法并使用@rafalkitta 或@josema 提供的答案。
But be cautious, this will be applied to all the search bars through out the app. In order to apply this approach to a particular search bar: create a subclass of UISearchBar
and set the defaultTextAttributes
on the UITextField
like below
但要小心,这将应用于整个应用程序的所有搜索栏。为了这种方法适用于一个特定的搜索栏:创建的子类UISearchBar
,并设置defaultTextAttributes
在UITextField
像下面
Swift4:
斯威夫特4:
let defaultTextAttribs = [NSAttributedStringKey.font.rawValue: UIFont.boldSystemFont(ofSize: 21.0), NSAttributedStringKey.foregroundColor.rawValue:UIColor.red]
UITextField.appearance(whenContainedInInstancesOf: [CustomSearchBar.self]).defaultTextAttributes = defaultTextAttribs
Obj-C(iOS11)
对象-C(iOS11)
UIFont *font = [UIFont boldSystemFontOfSize:21.0];
UIColor *color = [UIColor redColor];
NSDictionary * defaultTextAttribs = @{NSFontAttributeName:font, NSForegroundColorAttributeName: color};
[UITextField appearanceWhenContainedInInstancesOfClasses:@[[CustomSearchBar class]]].defaultTextAttributes = defaultTextAttribs;
回答by Adriano Spadoni
You can use KVC (key-Value Coding) to get the textfield
您可以使用 KVC(键值编码)来获取文本字段
UITextField *textField = [self.searchBar valueForKey: @"_searchField"];
[textField setTextColor:[UIColor redColor]];
[textField setFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:17.0]];
回答by Ciprian Rarau
In swift 2.0 for a search bar created programatically you could do this:
在以编程方式创建的搜索栏的 swift 2.0 中,您可以执行以下操作:
override func layoutSubviews() {
super.layoutSubviews()
let textFieldInsideSearchBar = self.searchBar.valueForKey("searchField") as! UITextField
textFieldInsideSearchBar.font = UIFont.systemFontOfSize(20)
}
In this case I put the code in a UIView subclass but it will work in other places too (i.e. viewWillAppear from the UIViewController)
在这种情况下,我将代码放在 UIView 子类中,但它也可以在其他地方工作(即 UIViewController 中的 viewWillAppear)
回答by Ashildr
In swift 3 , you can achieve this by :
在 swift 3 中,您可以通过以下方式实现:
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).font = UIFont(name: "AvenirNextCondensed-Regular", size: 17.0)
回答by rafalkitta
You shouldn't use private API like in other answers. If you want to customise search bars in whole application, you can use class's appearance proxy. It allows to modify search bar text attributes, placeholder attributes, cancel button, text field background image, background image etc. Example of use:
您不应该像其他答案一样使用私有 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"
}
回答by Eugene
UITextField
appearance does not work for UISearchBar
s created programmatically. You have to use a recursive workaround
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];
}
}
回答by A.G
I wanted to use 'Roboto-Regular' as default font for all the uisearchbars in my project. I made an uisearchbar extension.
我想使用“Roboto-Regular”作为我项目中所有 uisearchbars 的默认字体。我做了一个 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!
}
}
Usage:
用法:
self.sampleSearchBar.addRobotoFontToSearchBar(sampleSearchBar)