有没有办法在不继承搜索栏的情况下更改 UISearchBar 取消按钮的文本字体和颜色?
11 回答
UIBarButtonItem
您可以通过更改when 包含在 中的外观来更改取消按钮样式UISearchBar
。
例如,
[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor blueColor],
UITextAttributeTextColor,
[UIColor darkGrayColor],
UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, -1)],
UITextAttributeTextShadowOffset,
nil]
forState:UIControlStateNormal];
斯威夫特 3
let attributes = [
NSForegroundColorAttributeName : UIColor.white,
NSFontAttributeName : UIFont.systemFont(ofSize: 17)
]
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(attributes, for: .normal)
斯威夫特 5
let attributes:[NSAttributedString.Key: Any] = [
.foregroundColor: UIColor.black,
.font: UIFont.systemFont(ofSize: 17)
]
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(attributes, for: .normal)
如果您只想修改取消按钮,您可以通过以下方式进行:
if let cancelButton = searchBar.valueForKey("cancelButton") as? UIButton {
cancelButton.setTitle(<your_string>, forState: <UIControlState>)
cancelButton.setTitleColor(<your_uicolor>, forState: <UIControlState>)
cancelButton.setAttributedTitle(<your_nsattributedstring>, forState: <UIControlState>)
}
searchBar
你的UISearchBar
对象在哪里。
根据 htinlinn 的回答,这是我在viewDidLoad
使用 iOS 7 中的搜索栏的视图控制器方法中使用的:
[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil]
setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, nil]
forState:UIControlStateNormal];
@htinlinn 答案的简化 Swift 版本:
let attributes = [
NSForegroundColorAttributeName : UIColor.textBlueColor,
NSFontAttributeName : UIFont.systemFontOfSize(13)
]
UIBarButtonItem.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).setTitleTextAttributes(attributes, forState: .Normal)
要修改搜索栏取消按钮,您需要使用 Button 对象并将该按钮的引用更改为您的自定义按钮。
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(50,20,300,30)]; searchBar.delegate = self; searchBar.barStyle = UIBarStyleDefault; searchBar.showsCancelButton = YES; UIButton *cancelButton; for (id button in searchBar.subviews) { if ([button isKindOfClass:[UIButton class]]) { cancelButton=(UIButton*)button; break; } }
可以使用 searchBar 的 tint 属性来改变 searchBar 的颜色,取消按钮的颜色会根据 UISearchBar 的颜色而改变。我无法手动编辑。但是您始终可以在界面构建器中对其进行自定义,这将隐藏本机取消按钮。并且用户将使用您的自定义按钮作为 searchBar 的取消按钮。
iOS 9.0 之后的 Swift 解决方案(通过修改 htinlinn 的答案):
if #available(iOS 9.0, *) {
UIBarButtonItem.appearanceWhenContainedInInstancesOfClasses([UISearchBar.classForCoder()])
.setTitleTextAttributes([
NSFontAttributeName: UIFont.systemFontOfSize(12),
NSForegroundColorAttributeName: UIColor.blueColor(),
NSShadowAttributeName: NSShadow(color: UIColor.redColor(), offset: CGSizeMake(0, -1), blurRadius: 2)
],
forState: UIControlState.Normal)
} else {
// Link to Objective-C Method
}
而当前的 Objective-C 方法(UITextAttributeTextColor
自 iOS 7.0 起已弃用):
[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil]
setTitleTextAttributes:@{
NSForegroundColorAttributeName: [UIColor blueColor],
NSFontAttributeName: [UIFont systemFontOfSize:12]}
forState:UIControlStateNormal];
上面代码中使用的我的小影子扩展:
extension NSShadow {
convenience init(color: AnyObject!, offset: CGSize, blurRadius: CGFloat) {
self.init()
self.shadowColor = color
self.shadowOffset = offset
self.shadowBlurRadius = blurRadius
}
}
这里有一个更好的解决方案
UIButton *cancelButton = [searchBar valueForKey:@"_cancelButton"];
[cancelButton setTitleColor:[UIColor yourColor] forState:UIControlStateNormal];
[cancelButton setTitle:@"Your Text" forState:UIControlStateNormal];
同样,您可以更改按钮的其他样式和文本属性。
对于 iOS 13。*(@AnujAroshA 在评论中回答)
UIButton *cancelButton = [searchBar valueForKey:@"cancelButton"];
试试这个:
[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitleTextAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"SourceSansPro-Regular" size:14]} forState:UIControlStateNormal];