21

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

4

11 回答 11

73

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];
于 2012-07-20T04:21:56.737 回答
19

斯威夫特 3

let attributes = [
    NSForegroundColorAttributeName : UIColor.white,
    NSFontAttributeName : UIFont.systemFont(ofSize: 17)
]
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(attributes, for: .normal)
于 2016-12-08T14:54:35.047 回答
12

斯威夫特 5

let attributes:[NSAttributedString.Key: Any] = [
    .foregroundColor: UIColor.black,
    .font: UIFont.systemFont(ofSize: 17)
]
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(attributes, for: .normal)
于 2018-01-18T20:04:00.773 回答
8

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

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对象在哪里。

于 2016-06-21T23:33:13.453 回答
5

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

[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil]
                     setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, nil] 
                                   forState:UIControlStateNormal];
于 2014-02-23T03:10:32.553 回答
1

@htinlinn 答案的简化 Swift 版本:

let attributes = [
    NSForegroundColorAttributeName : UIColor.textBlueColor,
    NSFontAttributeName : UIFont.systemFontOfSize(13)
]
UIBarButtonItem.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).setTitleTextAttributes(attributes, forState: .Normal)
于 2016-07-11T14:49:41.773 回答
0

要修改搜索栏取消按钮,您需要使用 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;
   }
}
于 2012-07-20T04:11:09.830 回答
0

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

于 2012-07-20T04:15:30.320 回答
0

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
    }
}
于 2016-02-25T13:10:19.850 回答
-1

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

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"];
于 2015-11-26T10:43:41.627 回答
-1

试试这个:

[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitleTextAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"SourceSansPro-Regular" size:14]} forState:UIControlStateNormal];
于 2016-12-28T04:35:35.327 回答