user2560886
user2560886

Reputation: 2050

Setting UITextView height to a variable number of lines

I have a a UITextView populated with a paragraph of text with variable height at runtime.

At runtime, how do I adjust the height of UITextView to the height of a variable number of lines (not necessarily equal to the number lines in the paragraph of text)?

For example:

The analogue in CSS would be to set the view height to 2em if the text is "small", or to 5em if the text is "large", or in Android setting maxLines on a TextView at runtime.

Upvotes: 0

Views: 129

Answers (1)

Mohammadalijf
Mohammadalijf

Reputation: 1377

you can use autolayout. first you create a height constraint for your UITextView. then you outlet this constraint to your controller. then you can manipulate height from controller anytime you like.

 @IBOutlet weak var heightConstraint: NSLayoutConstraint!
 override func viewDidLoad() {
        super.viewDidLoad()

        heightConstraint.constant = 200 // or something else

    }

viewcontroller

Upvotes: 1

Related Questions