change the labelstyle of a button in swift

To change the label style of a UIButton in Swift, you need to use the setTitle method with the NSAttributedString class to set the text of the button. Here's an example:

main.swift
let button = UIButton()
let attributes: [NSAttributedString.Key: Any] = [
    .font: UIFont.systemFont(ofSize: 16),
    .foregroundColor: UIColor.white,
    .underlineStyle: NSUnderlineStyle.single.rawValue
]
let attributedTitle = NSAttributedString(string: "My Button", attributes: attributes)
button.setAttributedTitle(attributedTitle, for: .normal)
352 chars
9 lines

In the example above, the button's label style is changed by creating an NSAttributedString that defines the text's font, color, and underline style, and then using the setAttributedTitle method on the button to set the attributed text. You can customize the attributes dictionary to change the label style however you like.

gistlibby LogSnag