iosswiftuilabelsnapkit

Autoresize multiline UILabel in Swift


I have tried everything to auto-resize my UILabel in Swift. I can autoresize the text when it is one line but when it is two lines it no longer works. The text in the UILabel changes often happening about every 10 seconds. The code I have tried is:

let direction = UILabel(frame: CGRect(x: 95, y: 10, width: screenWidth - 95, height:100))
direction.numberOfLines = 0
direction.adjustsFontSizeToFitWidth = true
direction.lineBreakMode = .byWordWrapping
direction.textAlignment = .center
direction.minimumScaleFactor = 0.2
direction.font = UIFont.boldSystemFont(ofSize: 40)
view.addSubview(direction)
direction.text = "Ffafdafafdfa fafda  dfafaf afa"

func updateDirection(update: String){
    direction.text = update
} 

The original text "Ffafdafafdfa fafda dfafaf afa" will automatically resize but when updateDirection is called the font size with not be changed from 40. I have also tried setting the number of lines to 2 and removing the .byWordWrapping. How can I get my UILabel to resize automatically?


Solution

  • Below code will keep the frame size and adjust the font size according with direction label content.

    let backgroundView = UIView(frame: CGRect(x: 5, y: UINavigationController().navigationBar.frame.height + UIApplication.shared.statusBarFrame.height, width: UIScreen.main.bounds.width - 10, height: UIScreen.main.bounds.width - 100))
    let direction = UILabel()
    
     override func viewDidLoad() {
        super.viewDidLoad()
    
     direction.backgroundColor = UIColor.green
     direction.numberOfLines = 0
     direction.textAlignment = .center
     direction.font = UIFont.boldSystemFont(ofSize: 40)
     direction.adjustsFontForContentSizeCategory = true
     direction.adjustsFontSizeToFitWidth = true
     direction.text = "This is some multiline label with a background colour" // Set or Initiate random function for your array here.
    
     backgroundView.backgroundColor = UIColor.red
     view.addSubview(backgroundView)
     backgroundView.addSubview(direction)
    
     Timer.scheduledTimer(timeInterval: 10.0, target: self, selector: #selector(random), userInfo: nil, repeats: true)  
    
     direction.translatesAutoresizingMaskIntoConstraints = false
    
     NSLayoutConstraint(item: direction,
                           attribute: .leading,
                           relatedBy: .equal,
                           toItem: backgroundView,
                           attribute: .leadingMargin,
                           multiplier: 1.0,
                           constant: 0.0).isActive = true
    
    NSLayoutConstraint(item: direction,
                           attribute: .trailing,
                           relatedBy: .equal,
                           toItem: backgroundView,
                           attribute: .trailingMargin,
                           multiplier: 1.0,
                           constant: 0.0).isActive = true
    
    
    NSLayoutConstraint(item: direction,
                           attribute: .top,
                           relatedBy: .equal,
                           toItem: backgroundView,
                           attribute: .topMargin,
                           multiplier: 1.0,
                           constant: 0.0).isActive = true
    
    NSLayoutConstraint(item: direction,
                           attribute: .bottom,
                           relatedBy: .equal,
                           toItem: backgroundView,
                           attribute: .bottomMargin,
                           multiplier: 1.0,
                           constant: 0.0).isActive = true
    
    
    }
    
    func random(sender: Timer) {
    
        //Place your random func code here.     
    }
    

    Output:

    enter image description here