Label auto fit with Swift text size?

Asked

Viewed 726 times

4

I have a label inside a scrollView, and this label gets an N text, some are small and some are big so the idea was to put in scrollView, but the problem is that I can’t adjust the label to increase as the text, it cuts the rest of the line. The scrollView is not big is just a small part of the screen.

I’ve tried that in the code, but it didn’t work

 @IBOutlet weak var svPergunta: UIScrollView!
@IBOutlet weak var lblPergunta: UILabel!

var sPergunta = String()

override func viewDidLoad() {
        super.viewDidLoad()

    lblPergunta.adjustsFontSizeToFitWidth = true
    lblPergunta.text = sPergunta
    lblPergunta.lineBreakMode = NSLineBreakMode.ByWordWrapping
    svPergunta.contentSize.height = 200

}

1 answer

1

The alternative I found for my problem and in the case much better than the Label was to use Textview so I eliminated Scrollview also using only one component on the screen.

import UIKit

class ViewPerguntas: UIViewController {


@IBOutlet weak var lblPergunta: UITextView!

var sPergunta = String()

override func viewDidLoad() {
        super.viewDidLoad()

    lblPergunta.text = sPergunta
    lblPergunta.editable = false
    lblPergunta.selectable = false

   }

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.