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

时间:2012-07-20 03:18:30

标签: ios uisearchbar

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

11 个答案:

答案 0 :(得分:67)

您可以通过更改UIBarButtonItem中包含的UISearchBar的外观来更改取消按钮样式。

例如,

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

答案 1 :(得分:13)

Swift 3

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

答案 2 :(得分:4)

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

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

答案 3 :(得分:2)

如果您只想修改取消按钮,可以执行以下操作:

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对象。

答案 4 :(得分:2)

Swift 4

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

答案 5 :(得分:1)

简化的Swift版@htinlinn回答:

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

答案 6 :(得分: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;
   }
}

答案 7 :(得分:0)

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

答案 8 :(得分:0)

iOS 9.0之后的Swift解决方案(通过修改htinlinn&#39;答案):

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方法(自iOS 7.0以来已弃用UITextAttributeTextColor):

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

答案 9 :(得分:-1)

更好的解决方案here

>firebase init

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

答案 10 :(得分:-2)

试试这个:

[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitleTextAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"SourceSansPro-Regular" size:14]} forState:UIControlStateNormal];