Modifying UISearchBar Cancel button font text color and style
You can change Cancel button styling by changing the appearance of UIBarButtonItem
when contained in UISearchBar
.
For example,
[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor blueColor],
UITextAttributeTextColor,
[UIColor darkGrayColor],
UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, -1)],
UITextAttributeTextShadowOffset,
nil]
forState:UIControlStateNormal];
Swift 5
let attributes:[NSAttributedString.Key: Any] = [
.foregroundColor: UIColor.black,
.font: UIFont.systemFont(ofSize: 17)
]
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(attributes, for: .normal)
Swift 3
let attributes = [
NSForegroundColorAttributeName : UIColor.white,
NSFontAttributeName : UIFont.systemFont(ofSize: 17)
]
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(attributes, for: .normal)