我正在尝试将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)
有没有办法做到这一点?
我正在尝试将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)
有没有办法做到这一点?
尝试这样的事情,它为我工作。
UIBarButtonItem.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).setTitleTextAttributes([NSFontAttributeName : UIFont(name: "OpenSans", size: 15)!], forState: .Normal)
如果您想更改颜色,请将其添加到您的属性数组中。
NSForegroundColorAttributeName : UIColor.whiteColor()
注:在iOS中9可用
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];
这是Swift 3.0
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes([NSFontAttributeName : UIFont(name: "OpenSans", size: 15)!], for: .normal)
你不会碰巧有这样的快速等值吗? –
美丽的男人。非常感谢。您可能想要注意'appearanceWhenContainedInInstancesOfClasses'仅适用于iOS 9.0> –
@JLO我不确定您的意思,这是很快的。 –