0

I am trying to get the content height of an UITextView for calculating the bottom position of it (y position + height). I can get the y position just fine with

travelDescriptionTextView.frame.origin.y

and need to determinate the height. All over the internet it says to get the content height with:

let contentSize = self.travelDescriptionTextView.sizeThatFits(self.travelDescriptionTextView.bounds.size)
let height = contentSize.height

but this technique doesn't work when height is resized (extended) by word wrapping, that is, if a sentence is wider than the width of the text box and the textbox creates a new line automatically. The above technique for getting content height only get the height right if there is no word wrapping, or else the height is excluding the extra word wrapping lines causing the content height to be shorter than the actual content height.

So how do I get the content height of a UITextView containing word wrapping height resizes?

swiftBoy
  • 35,607
  • 26
  • 136
  • 135
lyzz
  • 128
  • 1
  • 9
  • 1
    please check http://stackoverflow.com/questions/19049532/how-do-i-size-a-uitextview-to-its-content-on-ios-7 – swiftBoy Mar 23 '16 at 09:46

2 Answers2

0

Try like as follows,

let textViewComputedHeight = textView.contentSize.height -  textView.contentInset.top - textView.contentInset.bottom
Venk
  • 5,949
  • 9
  • 41
  • 52
0

Thanks to RDC for the link. The working solution is:

            self.travelDescriptionTextView.sizeToFit()
            self.travelDescriptionTextView.layoutIfNeeded()
            let contentSize = self.travelDescriptionTextView.sizeThatFits(self.travelDescriptionTextView.bounds.size)
            let textViewHeight = contentSize.height
lyzz
  • 128
  • 1
  • 9