简体   繁体   中英

Setting UITextView height to a variable number of lines

I have aa 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:

  • Set UITextView to the height of 2 lines of text for some cases of text, then
  • Set UITextView to the height of 5 lines of text for other cases of text (with a scroll-bar to scroll for the other lines as is default behavior)

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.

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

    }

视图控制器

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM