点击栏按钮项目时 UIFont 发生变化 swift

UIFont changes when tapped on bar button item swift

我为 barButtonItem、UIColor、自定义字体设置了 setTitleTextAttributes。 当我点击 barButton 时,在我按住 barButton 的那段时间里,字体更改为系统字体,在我放开 barButton 之后它 return 到自定义 font.I 不要使用 tintColor 设置。为什么会这样?

有人可以帮助我吗?

这是我设置属性的方式:

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

我使用自定义 UIFont。提前致谢。

您也需要为高亮状态设置相同的字体:

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

在iOS15中可以使用这行代码:

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