Home > OS >  UIBarButtonItem text two colours
UIBarButtonItem text two colours

Time:03-19

i'm trying to find solution to change my UIBarButTonItem text in two colours , i find some functions for UILabel, i change it by my task:

extension NSMutableAttributedString {
    func setColorForText(textForAttribute: String, withColor color: UIColor) {
        let range: NSRange = self.mutableString.range(of: textForAttribute, options: .caseInsensitive)
        self.addAttribute(NSAttributedString.Key.foregroundColor, value: color, range: range)
    }
}
extension UILabel{
    func setTwoColors(firstWord: String, secondWord: String , firstColor:UIColor = .white , secondColor: UIColor = .blue) {
        let attributedString: NSMutableAttributedString = NSMutableAttributedString(string: firstWord secondWord)
        attributedString.setColorForText(textForAttribute: firstWord, withColor: firstColor)
        attributedString.setColorForText(textForAttribute: secondWord, withColor: secondColor)
        self.attributedText = attributedString
    }
}

But I can't really find solution to change UIBarButtonItem to looks like this : enter image description here

  • Related