ios 修改 UISearchBar 取消按钮字体文字颜色和样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11572372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:20:00  来源:igfitidea点击:

Modifying UISearchBar Cancel button font text color and style

iosuisearchbar

提问by Rowie Po

Is there any way to change the text font and color of the UISearchBar Cancel button without subclassing the searchbar?

有没有办法在不继承搜索栏的情况下更改 UISearchBar 取消按钮的文本字体和颜色?

回答by htinlinn

You can change Cancel button styling by changing the appearance of UIBarButtonItemwhen contained in UISearchBar.

您可以通过更改UIBarButtonItem包含在UISearchBar.

For example,

例如,

[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                      [UIColor blueColor], 
                                                      UITextAttributeTextColor, 
                                                      [UIColor darkGrayColor], 
                                                      UITextAttributeTextShadowColor, 
                                                      [NSValue valueWithUIOffset:UIOffsetMake(0, -1)], 
                                                      UITextAttributeTextShadowOffset,
                                                      nil] 
                                            forState:UIControlStateNormal];

回答by Chris Van Buskirk

Swift 3

斯威夫特 3

let attributes = [
    NSForegroundColorAttributeName : UIColor.white,
    NSFontAttributeName : UIFont.systemFont(ofSize: 17)
]
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(attributes, for: .normal)

回答by Javier Cancio

Swift 5

斯威夫特 5

let attributes:[NSAttributedString.Key: Any] = [
    .foregroundColor: UIColor.black,
    .font: UIFont.systemFont(ofSize: 17)
]
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(attributes, for: .normal)

回答by danielM

Based on htinlinn's answer, this is what I used in my viewDidLoadmethod of the view controller using the search bar in iOS 7:

基于 htinlinn 的回答,这是我在viewDidLoad使用 iOS 7 中的搜索栏的视图控制器方法中使用的:

[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil]
                     setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, nil] 
                                   forState:UIControlStateNormal];

回答by eMdOS

If you just want to modify Cancelbutton, you can do it by:

如果您只想修改取消按钮,可以通过以下方式进行:

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>)
}

where searchBaris your UISearchBarobject.

searchBar你的UISearchBar对象在哪里。

回答by Vinícius Rodrigues

Simplified Swift version of @htinlinn answer:

@htinlinn 答案的简化 Swift 版本:

let attributes = [
    NSForegroundColorAttributeName : UIColor.textBlueColor,
    NSFontAttributeName : UIFont.systemFontOfSize(13)
]
UIBarButtonItem.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).setTitleTextAttributes(attributes, forState: .Normal)

回答by Manish Agrawal

For modifying the search bar cancel button you have take Button object and change the reference of that button to your custom button.

要修改搜索栏取消按钮,您需要获取 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;
   }
}
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;
   }
}

回答by Saleh

You can use the tint property of searchBar to change the color of searchBar, cancel button's color will get changed but according to the color of UISearchBar. I can't be edited manually. But you can always put a custom over it in the interface builder which will hide the native cancel button. And the user will use your custom button as the cancel button of the searchBar.

您可以使用searchBar 的tint 属性来更改searchBar 的颜色,取消按钮的颜色将根据UISearchBar 的颜色进行更改。我无法手动编辑。但是您始终可以在界面构建器中对其进行自定义,这将隐藏本机取消按钮。用户将使用您的自定义按钮作为搜索栏的取消按钮。

回答by Kádi

Swift solution after iOS 9.0 (by modifying htinlinn's answer):

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
}

And the current Objective-C method (UITextAttributeTextColoris deprecated since iOS 7.0):

而当前的 Objective-C 方法(UITextAttributeTextColor自 iOS 7.0 起已弃用):

[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil]
 setTitleTextAttributes:@{
                          NSForegroundColorAttributeName: [UIColor blueColor],
                          NSFontAttributeName: [UIFont systemFontOfSize:12]}
 forState:UIControlStateNormal];

My little shadow extension used in the code above:

我在上面的代码中使用的小影子扩展:

extension NSShadow {
    convenience init(color: AnyObject!, offset: CGSize, blurRadius: CGFloat) {
        self.init()
        self.shadowColor = color
        self.shadowOffset = offset
        self.shadowBlurRadius = blurRadius
    }
}

回答by Burhanuddin Sunelwala

A much better solution here

这里有一个更好的解决方案

UIButton *cancelButton = [searchBar valueForKey:@"_cancelButton"];
[cancelButton setTitleColor:[UIColor yourColor] forState:UIControlStateNormal];
[cancelButton setTitle:@"Your Text" forState:UIControlStateNormal];

and similarly you can change other style and text properties of the button.

同样,您可以更改按钮的其他样式和文本属性。

For iOS 13.* (@AnujAroshA answer in comments)

对于 iOS 13.*(@AnujAroshA 在评论中回答)

UIButton *cancelButton = [searchBar valueForKey:@"cancelButton"];