I have a
#IBOutlet weak var searchBar: UISearchBar!
and I'm trying to change the font and color of the cancel button with but it's not working. The code I'm using is the following:
fileprivate static func applyStyle() {
let appearance = UIBarButtonItem.appearance()
let commonAttributes: [NSAttributedString.Key: Any] = [
.font: UIFont.bodySmall,
.foregroundColor: ColorEnum.black,
.kern: NSNumber(value: Float(NLLetterSpacing.bodySmall))
]
appearance.setTitleTextAttributes(commonAttributes, for: .normal)
appearance.setTitleTextAttributes(commonAttributes, for: .highlighted)
appearance.setTitleTextAttributes([
.font: UIFont.bodySmall,
.foregroundColor: ColorEnum.pinkishGrey,
.kern: NSNumber(value: Float(NLLetterSpacing.bodySmall))
], for: .disabled)
}
However, I also have a weak var searchController: UISearchController? and the code it's working for it's searchController.searchBar.
Adding
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(commonAttributes, for: .normal)
should do the trick, then you could easily get rid of the appearance
Related
enter image description here
I want to make button like this picture.
In order to implement it like a picture, the color of the button title needs to be partially changed, but I don't know what to do.
Here is my code.
private let signUpButton = UIButton().then {
$0.setTitleColor(.black, for: .normal)
$0.titleLabel?.font = .boldSystemFont(ofSize: 12)
}
Use NSMutableAttributedString for this purpose
private let signUpButton = UIButton().then {
// Creating gray text part
let grayButtonText = NSAttributedString(string: "FirstPart", attributes: [.font: UIFont.systemFont(ofSize: 12), .foregroundColor: UIColor.gray ])
// Creating black text part
let blackButtonText = NSAttributedString(string: "SecondPart", attributes: [.font: UIFont.systemFont(ofSize: 12), .foregroundColor: UIColor.black ])
// Merging two parts together
let buttonTitle = NSMutableAttributedString(attributedString: grayButtonText)
buttonTitle.append(blackButtonText)
// Set it as a title for ".normal" state
$0.setAttributedTitle(buttonTitle, for: .normal)
}
I have a barbuttonItem here:
let doneBarButtonItem: UIBarButtonItem? = {
let barButtonItem = UIBarButtonItem()
barButtonItem.setTitleTextAttributes([
NSAttributedString.Key.font : UIFont(name: "Avenir-Heavy", size: 12)!,
NSAttributedString.Key.foregroundColor : UIColor.black,
], for: .normal)
barButtonItem.title = "DONE"
return barButtonItem
}()
Yet when i press on it, it changes to a different font. What is the property to change the font when highlighted? Thank you.
barButtonItem.setTitleTextAttributes([
NSAttributedString.Key.font : UIFont(name: "Avenir-Heavy", size: 12)!,
NSAttributedString.Key.foregroundColor : UIColor.black,
], for: .highlighted) // This line
I changed font of navigation bar title and back button in AppDelegate following way
private func setupNavigationBar() {
//Navigation Bar
let attrs: [NSAttributedStringKey: Any] = [
NSAttributedStringKey.foregroundColor: UIColor.white,
NSAttributedStringKey.font: FontFamily.Muller.medium.font(size: 17)
]
UIApplication.shared.statusBarStyle = .lightContent
UINavigationBar.appearance().isTranslucent = false
UINavigationBar.appearance().backgroundColor = ColorName.mainBlue.color
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().titleTextAttributes = attrs
let barButtonItemAppearance = UIBarButtonItem.appearance()
barButtonItemAppearance.setTitleTextAttributes([NSAttributedStringKey.font: FontFamily.Muller.regular.font(size: 15)], for: .normal)
barButtonItemAppearance.setTitleTextAttributes([NSAttributedStringKey.font:
FontFamily.Muller.regular.font(size: 15)], for: .highlighted)
}
And it's ok
But frame of back button breaks on 3rd screen.
Any ideas how I can fix this?
I use this code in appDelegate to change color of all UIBarButtonItem of my navigationBar in my project to white
let whiteAttr = [NSAttributedStringKey.font : UIFont(name: "OpenSans", size: 14)! ,NSAttributedStringKey.foregroundColor: UIColor.white]
UIBarButtonItem.appearance().setTitleTextAttributes(whiteAttr, for: .normal)
UIBarButtonItem.appearance().setTitleTextAttributes(whiteAttr, for: .highlighted)
But Done button of my ToolBar in WKWebView is also white, how can i change the color ?
Just got this working using the appearance APIs from iOS 13:
let doneButtonAppearance = UIBarButtonItemAppearance()
doneButtonAppearance.normal.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.blue]
let toolBarAppearance = UIToolbarAppearance()
toolBarAppearance.doneButtonAppearance = doneButtonAppearance
UIToolbar.appearance().standardAppearance = toolBarAppearance
// Unsure if this is needed
if #available(iOS 15.0, *) {
UIToolbar.appearance().scrollEdgeAppearance = toolBarAppearance
}
let BarButtonItemAppearance = UIBarButtonItem.appearance()
BarButtonItemAppearance.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.blue], for: .normal)
I made a custom barButtonItem in my toolbar. I want to change the string of the NSAttributedString when the barButtonItem has been tapped. But as the apple document said the performance of the function is charged by the customView, not the barButtonItem any more, how can I access and do some change to the details of it according to events?
let button1 = UIButton(type: .System)
button1.sizeToFit()
let attributes1 = [NSForegroundColorAttributeName: UIColor.grayColor(), NSFontAttributeName: UIFont.systemFontOfSize(13)]
let attributedString1 = NSAttributedString(string: "Hello", attributes: attributes1)
button1.setAttributedTitle(attributedString1, forState: .Normal)
button1.addTarget(self, action: #selector(ActionViewController.switchAccounts), forControlEvents: .TouchUpInside)
let account = UIBarButtonItem(customView: button1)
setToolbarItems([account], animated: true)
I'm a new swifter. OC can also be read by me.
Any advice is welcome. Thanks.
you can use your custom button action method like this
func switchAccounts(sender : UIButton) {
let attributes1 = [NSForegroundColorAttributeName: UIColor.grayColor(), NSFontAttributeName: UIFont.systemFontOfSize(13)]
let attributedString1 = NSAttributedString(string: "world!", attributes: attributes1)
sender.setAttributedTitle(attributedString1, forState: .Normal)
sender.sizeToFit()
}
and
button1.addTarget(self, action: #selector(ActionViewController.switchAccounts(_:))
instead of
button1.addTarget(self, action: #selector(ActionViewController.switchAccounts)
Has anyone found that stackoverflow can be used as a little yellow duck? lol
for item in toolbarItems! where item.tag == 1000 {
let attributes1 = [NSForegroundColorAttributeName: UIColor.grayColor(), NSFontAttributeName: UIFont.systemFontOfSize(13)]
let attributedString1 = NSAttributedString(string: "world!", attributes: attributes1)
let button = item.customView as! UIButton
button.setAttributedTitle(attributedString1, forState: .Normal)
button.sizeToFit()
}
change target code as below.
button1.addTarget(self, action: #selector(ActionViewController.switchAccounts(_:)), forControlEvents: .TouchUpInside)
Make switchAccounts() like below.
func switchAccounts(sender:UIButton){
sender.selected = !sender.selected
let attributes1 = [NSForegroundColorAttributeName: UIColor.redColor(), NSFontAttributeName: UIFont.systemFontOfSize(13)]
let attributedString1 = NSAttributedString(string: "Hello click", attributes: attributes1)
sender.setAttributedTitle(attributedString1, forState: .Selected)
}