如何在iOS 7中更改UISearchBar的字体大小和字体样式?
UITextField *textField = [[searchBar subviews] objectAtIndex:1];
[textField setFont:[UIFont fontWithName:@"Helvetica" size:20]];
在iOS 6中工作,但它在iOS 7中崩溃
答案 0 :(得分:67)
试试这个,它适用于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],
}];
}
答案 1 :(得分:34)
在iOS 7.1上,我接受的答案对我不起作用。我不得不改变它:
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setDefaultTextAttributes:@{
NSFontAttributeName: [UIFont fontWithName:@"Avenir-Heavy" size:20.],
}];
答案 2 :(得分:6)
iOS 10 Swift 3:
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSFontAttributeName:UIFont(name: "Avenir-Heavy", size: 22)!]
答案 3 :(得分:2)
appearanceWhenContainedIn:
,请使用以下命令:
[[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setDefaultTextAttributes:@{ NSFontAttributeName: [UIFont fontWithName:@"FontName" size:14]}];
答案 4 :(得分:1)
正如 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
的索引。
答案 5 :(得分:1)
对于那些正在寻找有效的Swift版本的人I've adapted this answer。 Swift目前不支持Objc varargs方法,因此它不能直接使用上述方法。我们可以通过制作一个不使用varargs并调用我们需要的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)
答案 6 :(得分:1)
对于Swift 4,您需要引用NSAttributedStringKey
枚举:
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self])
.defaultTextAttributes = [NSAttributedStringKey.font.rawValue: UIFont(...)]
答案 7 :(得分:0)
对我来说,这可以使用递归函数来查找实际的文本字段。
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) }
}
}
}
答案 8 :(得分:0)
在Swift 5中
@Piotr Tomasik的代码在更改位后仍能在Swift 5中运行
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSAttributedString.Key.font:UIFont(name: "poppins", size: 13)!]