簡體   English   中英

修改 UISearchBar 取消按鈕字體文字顏色和樣式

[英]Modifying UISearchBar Cancel button font text color and style

有沒有辦法在不繼承搜索欄的情況下更改 UISearchBar 取消按鈕的文本字體和顏色?

當包含在UISearchBar時,您可以通過更改UIBarButtonItem的外觀來更改取消按鈕樣式。

例如,

[[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 的回答,這是我在使用 iOS 7 中的搜索欄的視圖控制器的viewDidLoad方法中使用的:

[[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 的顏色進行更改。 我無法手動編輯。 但是您始終可以在界面構建器中對其進行自定義,這將隱藏本機取消按鈕。 用戶將使用您的自定義按鈕作為搜索欄的取消按鈕。

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];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM