2016-06-10 56 views
4

我正在嘗試將UISearchBar取消按鈕的字體更改爲「OpenSans」,但我無法訪問任何屬性。我只能改變的UISearchBar的色調的顏色,改變搜索欄的顏色取消按鈕文本&搜索欄中的UITextField光標顏色:更改UISearchBar的字體樣式取消按鈕

searchBar.tintColor = UIColor(red: 187.0/255.0, green: 187.0/255.0, blue: 187.0/255.0, alpha: 1.0) 

有沒有辦法做到這一點?

回答

7

嘗試這樣的事情,它爲我工作。

UIBarButtonItem.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).setTitleTextAttributes([NSFontAttributeName : UIFont(name: "OpenSans", size: 15)!], forState: .Normal) 

如果您想更改顏色,請將其添加到您的屬性數組中。

NSForegroundColorAttributeName : UIColor.whiteColor() 

注:在iOS中9可用

+0

你不會碰巧有這樣的快速等值嗎? –

+0

美麗的男人。非常感謝。您可能想要注意'appearanceWhenContainedInInstancesOfClasses'僅適用於iOS 9.0> –

+2

@JLO我不確定您的意思,這是很快的。 –

0

SWIFT 2:

if #available(iOS 9.0, *) { 
    UIBarButtonItem.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).setTitleTextAttributes([NSFontAttributeName : UIFont.systemFontOfSize(15, weight: UIFontWeightLight)], forState: .Normal) 
} else { 
    // Fallback on earlier versions 
} 

swift3:

UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes([NSFontAttributeName : UIFont(name: "OpenSans", size: 15)!], for: .normal) 

的Objective-C:

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

這是Swift 3.0

UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes([NSFontAttributeName : UIFont(name: "OpenSans", size: 15)!], for: .normal)