[英]Changing Cancel button Font in UISearchBar
我正在自定義搜索欄,並努力更改在搜索欄中鍵入內容時彈出的“取消”按鈕的字體。 到目前為止,這是我嘗試過的方法(將UIView廣播到UIBarButton失敗):
for subView in controller.searchBar.subviews {
if let cancelButton = subView as? UIBarButtonItem {
cancelButton.setTitleTextAttributes([ NSFontAttributeName: font ], forState: UIControlState.Normal)
}
}
迅速:
if #available(iOS 9.0, *) {
UIBarButtonItem.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).setTitleTextAttributes([NSFontAttributeName : UIFont.systemFontOfSize(15, weight: UIFontWeightLight)], forState: .Normal)
} else {
// Fallback on earlier versions
}
目標-C:
[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor blueColor],
UITextAttributeTextColor,
[UIColor darkGrayColor],
UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, -1)],
UITextAttributeTextShadowOffset,
nil]
forState:UIControlStateNormal];
在包含搜索欄的視圖中,例如:
class ViewController: UIViewController {
@IBOutlet weak var searchBar: UISearchBar!
// ...
override func viewDidLoad() {
super.viewDidLoad()
// ... other init code
// search bar "Cancel" color
searchBar.tintColor = UIColor.redColor();
// search bar "Cancel": custom text and font
for sView in searchBar.subviews {
for ssView in sView.subviews {
if ssView.isKindOfClass(UIButton.self) {
let cancelButton = ssView as! UIButton
cancelButton.setTitle("Custom", forState: .Normal)
cancelButton.titleLabel?.font = UIFont(name: "GillSans-BoldItalic", size: 14.0)
break
}
}
}
}
// ...
}
對於不同的字體,請參見http://iosfonts.com ;對於顏色,請參見《 UIColor類參考》 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.