我该如何在iOS 7中改变UISearchBar的字体大小和字体样式?
UITextField *textField = [[searchBar subviews] objectAtIndex:1];
[textField setFont:[UIFont fontWithName:@"Helvetica" size:20]];
iOS 6上可以工作,但在iOS 7上崩溃了
我该如何在iOS 7中改变UISearchBar的字体大小和字体样式?
UITextField *textField = [[searchBar subviews] objectAtIndex:1];
[textField setFont:[UIFont fontWithName:@"Helvetica" size:20]];
iOS 6上可以工作,但在iOS 7上崩溃了
试一下这个,对于 iOS 5.0 及以上版本可以正常工作:(iOS 7同样适用)
- (void)viewDidLoad
{
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setFont:[UIFont fontWithName:@"Helvetica" size:20]];
}
适用于 iOS 8
- (void)viewDidLoad
{
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setDefaultTextAttributes:@{
NSFontAttributeName: [UIFont fontWithName:@"Helvetica" size:20],
}];
}
对我来说,被接受的答案在iOS 7.1上无效。我不得不将其更改为以下内容:
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setDefaultTextAttributes:@{
NSFontAttributeName: [UIFont fontWithName:@"Avenir-Heavy" size:20.],
}];
iOS 10 Swift 3:
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSFontAttributeName:UIFont(name: "Avenir-Heavy", size: 22)!]
appearanceWhenContainedIn:
在iOS9中已经被弃用,请使用以下方式:
[[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setDefaultTextAttributes:@{ NSFontAttributeName: [UIFont fontWithName:@"FontName" size:14]}];
对于那些寻找工作的Swift版本的人,我已经适应了这个答案。Swift目前不支持Objc可变参数方法,因此不能直接使用上述方法。我们可以通过创建一个不使用可变参数并调用所需内容的Objective-C类别来解决这个问题:
// UIAppearance+Swift.h
@interface UIView (UIViewAppearance_Swift)
// appearanceWhenContainedIn: is not available in Swift. This fixes that.
+ (instancetype)my_appearanceWhenContainedIn:(Class<UIAppearanceContainer>)containerClass;
@end
—
// UIAppearance+Swift.m
@implementation UIView (UIViewAppearance_Swift)
+ (instancetype)my_appearanceWhenContainedIn:(Class<UIAppearanceContainer>)containerClass {
return [self appearanceWhenContainedIn:containerClass, nil];
}
@end
请确保在桥接头文件中导入#import "UIAppearance+Swift.h"
。
然后只需调用:
UITextField.my_appearanceWhenContainedIn(UISearchBar.self).font = UIFont.systemFontOfSize(14.0)
对于Swift 4,您需要引用NSAttributedStringKey
枚举:
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self])
.defaultTextAttributes = [NSAttributedStringKey.font.rawValue: UIFont(...)]
正如rmaddy所说,你不应该依赖于标准UI组件的私有子视图结构。这些会发生变化并导致代码崩溃。相反,你应该使用提供的API来完成这些操作。
我认为更加安全的方法是:
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setFont:[UIFont systemFontOfSize:14]];
for(UIView *subView in searchBar.subviews) {
if ([subView isKindOfClass:[UITextField class]]) {
UITextField *searchField = (UITextField *)subView;
searchField.font = [UIFont systemFontOfSize:14];
}
}
subviews
的index。UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSAttributedString.Key.font:UIFont(name: "poppins", size: 13)!]
对我来说,使用递归函数查找实际文本字段的方法很有效。
extension UIView {
func recursive_applyTheme_Search(
#dynamicTextStyle: NSString,
bgColor: UIColor,
cursorColor: UIColor,
textColor: UIColor,
placeholderTextColor: UIColor,
borderColor: UIColor,
borderWidth: CGFloat,
cornerRadius: CGFloat) {
for subview in self.subviews
{
if subview is UITextField {
(subview as! UITextField).applyThemeForSearchBar(
dynamicTextStyle: dynamicTextStyle,
bgColor: bgColor,
cursorColor: cursorColor,
textColor: textColor,
placeholderTextColor: placeholderTextColor,
borderColor: borderColor,
borderWidth: borderWidth,
cornerRadius: cornerRadius)
}
else { subview.recursive_applyTheme_Search(
dynamicTextStyle: dynamicTextStyle,
bgColor: bgColor,
cursorColor: cursorColor,
textColor: textColor,
placeholderTextColor: placeholderTextColor,
borderColor: borderColor,
borderWidth: borderWidth,
cornerRadius: cornerRadius) }
}
}
}