I have programmatically created a simple large UIButton that displays five lines of text with my Swift 5 code below.
Question
Using code, how do I change the attributes of the UIButton to either increase or decrease the line spacing between each line of text? For example, how do I create double line spacing or create half line spacing?
Code
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
addMyButton()
}
func addMyButton() {
let myButtonTitleLabelString = "First\nSecond\nThird\nFourth\nFifth"
let myButtonTitleFontName = "HelveticaNeue"
let myButtonTitleFontSize = CGFloat(20)
let myButton = UIButton(type: UIButton.ButtonType.custom)
myButton.setTitle(myButtonTitleLabelString, for:UIControl.State())
myButton.titleLabel!.textAlignment = .center
myButton.titleLabel!.lineBreakMode = .byWordWrapping
myButton.titleLabel!.font = UIFont(name: myButtonTitleFontName, size: myButtonTitleFontSize)
myButton.titleEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
myButton.setTitleColor(UIColor.white, for: UIControl.State())
myButton.backgroundColor = UIColor.orange
myButton.frame = CGRect(x: 40, y: 100, width: 300, height: 300)
view.addSubview(myButton)
}
}
