0

I want to make my bar button item text in a toolbar (or Nav bar) bold. I know how to change the font color manually with an NSAttributedString like below:

button.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor(red: 242/255, green: 133/255, blue: 94/255, alpha: 1.0)], for: .normal)

I also know how to change bold on something else like a tableview cell with UIFont.boldSystemFont(ofSize: 12), but this can't be used with the above.

Is there a simple way to set a UIBarButtonItem bold?

*Edit: other similar answered questions show just changing the font type to Helvetica-Bold using NSFontAttributeName: UIFont(name: "Helvetica-Bold", size: 12.0), but is there a way to have the actual system font bolded, or is Helvetica-Bold the exact equivalent of the system font bolded?

Adam S.
  • 173
  • 3
  • 15

1 Answers1

-3

i think it's help you

    extension NSMutableAttributedString {
    @discardableResult func bold(_ text: String) -> NSMutableAttributedString {
        let attrs: [NSAttributedStringKey: Any] = [.font: UIFont(name: ""AmericanTypewriter-Bold"", size: 12)!]
        let boldString = NSMutableAttributedString(string:text, attributes: attrs)
        append(boldString)

        return self
    }

    @discardableResult func normal(_ text: String) -> NSMutableAttributedString {
        let normal = NSAttributedString(string: text)
        append(normal)

        return self
    }
}

and use like below

    let formattedString = NSMutableAttributedString()
formattedString
    .bold("Bold Text")

button.setTitle(formattedString, for: .normal)
Gowtham Sooryaraj
  • 3,639
  • 3
  • 13
  • 22