Custom paragraph line spacing for UIButton in Swift

Viewed 17

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)
        
    }
     }
1 Answers

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)

//// Note: Remove these next 2 lines
        
//        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)
        

//// Note: Add these next 7 lines

//// Note: Change `myButtonParagraphStyleParagraphSpacing` `CGFloat` value to change line spacing; 
////       1 = single line spacing, 2 = double line spacing, 0.5 = half line spacing.

        let myButtonParagraphStyle = NSMutableParagraphStyle()
        let myButtonParagraphStyleParagraphSpacing = CGFloat(2)

        myButtonParagraphStyle.alignment = .center
        myButtonParagraphStyle.paragraphSpacing = (myButtonTitleFontSize * (myButtonParagraphStyleParagraphSpacing - 1))

        let myButtonAttributes = [NSAttributedString.Key.paragraphStyle: myButtonParagraphStyle]
        let myButtonAttributedText = NSAttributedString(string: myButtonTitleLabelString, attributes: myButtonAttributes)

        myButton.setAttributedTitle(myButtonAttributedText, for: UIControl.State())
        

        view.addSubview(myButton)
        
    }
    
}

Image

enter image description here

Related