如何更改字体大小UISearchBar
?
19 回答
我建议 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]}];
这样你就不需要为你的应用程序中的每个搜索栏枚举子视图了。
执行此操作的安全方法如下:
for(UIView *subView in searchBar.subviews) {
if ([subView isKindOfClass:[UITextField class]]) {
UITextField *searchField = (UITextField *)subView;
searchField.font = [UIFont fontWithName:@"Oswald" size:11];
}
}
为什么这比公认的答案更安全?因为它不依赖 UITextField 的索引保持不变。(它也是一个更清洁的 for 循环)
接受的答案不是为 UISearchBar 应用字体的推荐方法。最好避免这种方法并使用@rafalkitta 或@josema 提供的答案。
但要小心,这将应用于整个应用程序的所有搜索栏。为了将此方法应用于特定的搜索栏:创建一个子类并UISearchBar
设置如下defaultTextAttributes
UITextField
斯威夫特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)
UIFont *font = [UIFont boldSystemFontOfSize:21.0];
UIColor *color = [UIColor redColor];
NSDictionary * defaultTextAttribs = @{NSFontAttributeName:font, NSForegroundColorAttributeName: color};
[UITextField appearanceWhenContainedInInstancesOfClasses:@[[CustomSearchBar class]]].defaultTextAttributes = defaultTextAttribs;
UISearchBar 内部有一个 UITextField,但没有访问它的属性。因此,没有办法以标准方式进行。
但是有一种非标准的方法可以解决它。UISearchBar 继承自 UIView,您可以使用 [searchBar subviews] 访问它的子视图。如果您在控制台中打印它的子视图,您将看到它具有这些视图。
UISearchBarBackground,UISearchBarTextField。
因此,要更改字体大小,您只需要这样做
UITextField *textField = [[searchBar subviews] objectAtIndex:1];
[textField setFont:[UIFont fontWithName:@"Helvetica" size:40]];
但是,如果将来 UISearchBar 发生变化,并且 textfield 不再位于位置 1,您的程序将崩溃,因此最好通过子视图检查 UITextField 的位置,然后设置字体大小。
您可以使用 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 子类中,但它也可以在其他地方工作(即来自 UIViewController 的 viewWillAppear)
在 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"
}
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];
}
}
iOS 13+
searchBar.searchTextField.font = UIFont(name: "YOUR-FONT-NAME", size: 13)
我想使用“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!
}
}
用法:
self.sampleSearchBar.addRobotoFontToSearchBar(sampleSearchBar)
用 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)
}
}
完整的代码应该如下:
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 *searchField = [self.searchDisplayController.searchBar valueForKey:@"_searchField"];
searchField.font = [[UIFont fontWithName:@"Oswald" size:11];
如果您需要一种方便的方法来更改所有应用程序的搜索栏,这里有一个Swift 5中的简单子类:
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)
}
我知道这是一个旧线程,但是由于 iOS 13 UISearchBar 具有 searchTextField 属性,您可以在该属性上设置所需的字体。
对于 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
在您定义或设置 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]];