如何更改UISearchBar
的字体大小?
如何更改UISearchBar
的字体大小?
我建议iOS 5.0及以上版本采取另一种不同的选项:
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setFont:[UIFont systemFontOfSize:14]];
iOS 8的适用版本(由Mike Gledhill提供的链接):
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setDefaultTextAttributes:@{
NSFontAttributeName: [UIFont fontWithName:@"Helvetica" size:20],
}];
iOS 9及以上版本:
[[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setDefaultTextAttributes:@{NSFontAttributeName: [UIFont fontWithName:@"Helvetica" size:20]}];
这样你就不需要为应用中的每个搜索栏枚举子视图而烦恼了。
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [...]
即可。 - Evils执行此操作的安全方式如下:
for(UIView *subView in searchBar.subviews) {
if ([subView isKindOfClass:[UITextField class]]) {
UITextField *searchField = (UITextField *)subView;
searchField.font = [UIFont fontWithName:@"Oswald" size:11];
}
}
为什么这种方法比被接受的答案更安全呢?因为它不依赖于UITextField索引保持不变。(同时也是一个更干净的循环)
UITextField
的子视图。但即使如此,它也不会崩溃。 - Nicolas Miari被接受的答案不是推荐的在UISearchBar上应用字体的方法。最好避免此方法,并使用@rafalkitta或@josema提供的答案。
但要小心,这将应用于整个应用程序中的所有搜索栏。为了将此方法应用于特定的搜索栏:创建UISearchBar
的一个子类,并像下面这样设置UITextField
的defaultTextAttributes
。
Swift4:
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)
UIFont *font = [UIFont boldSystemFontOfSize:21.0];
UIColor *color = [UIColor redColor];
NSDictionary * defaultTextAttribs = @{NSFontAttributeName:font, NSForegroundColorAttributeName: color};
[UITextField appearanceWhenContainedInInstancesOfClasses:@[[CustomSearchBar class]]].defaultTextAttributes = defaultTextAttribs;
UITextField *textField = [[searchBar subviews] objectAtIndex:1];
[textField setFont:[UIFont fontWithName:@"Helvetica" size:40]];
但是,如果UISearchBar在将来发生更改,并且textfield不再位于位置1,则您的程序将崩溃,因此最好通过子视图检查UITextField的位置,然后设置字体大小。
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子类中,但它也可以在其他地方使用(例如从UIViewController的viewWillAppear方法中)在这个案例中,我把代码放在一个UIView子类中,但它也可以在其他地方使用(比如在UIViewController的viewWillAppear方法中)
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"
}
defaultTextAttributes
时,就不需要使用attributedPlaceholder
。 - Miroslav HrivikUITextField
的外观设置无法应用于以编程方式创建的UISearchBar
。您需要使用递归解决方法。
- (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];
}
}
UITextField
(或其子类)并且在子视图层次结构中的某个位置,它就不受版本影响。我不明白为什么你要使用 UIAppearance。为什么不直接设置属性呢? - DimaUIAppearance
,因为我首先通过外观代理设置了字体和文本颜色值,但是当您以编程方式创建搜索栏时,系统不会选择它,这就是为什么我正在使用此解决方法+来自外观的值,以便如果我决定在将来更改这些值,它们将在整个应用程序中得到更改。 - EugeneiOS 13+
searchBar.searchTextField.font = UIFont(name: "YOUR-FONT-NAME", size: 13)
UISearchBar
的视图层次结构(或者至少是我们关心的视图)。 - ZGski