'UIFont changes when tapped on bar button item swift

I set setTitleTextAttributes for barButtonItem, UIColor, Custom Font. When I tapped on barButton, during that time when I hold barButton, font changes to the system one, after I let go barButton it return to the custom font.I don't use tintColor settings. Why this happen?

May anyone help me?

This is how I set properties:

@IBOutlet weak var saveBarButton: UIBarButtonItem! { 
    didSet {
        saveBarButton.title = "SAVE"
        saveBarButton.setTitleTextAttributes([NSAttributedString.Key.font:
        UIFont.textStyle5], for: .normal)
    } 
}

I use custom UIFont. Thanks in advance.



Solution 1:[1]

You need to set the same font for the highlighted state as well:

saveBarButton.setTitleTextAttributes([NSAttributedString.Key.font: UIFont.textStyle5], for: .highlighted) 

Solution 2:[2]

In iOS 15 you can use this line of code:

saveBarButton.configuration?.attributedTitle?.font = UIFont.textStyle5

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 rodskagg
Solution 2 Mile Dev