The key it use textContainerInset
rather than contentInset
. This will give you the proper buffer on the top and bottom that you're looking for. So, you'll get something like:
textView.text = "Some multiline text";
let size: CGSize = textView.sizeThatFits(CGSizeMake(textView.frame.size.width, CGFloat.max));
let insets: UIEdgeInsets = textView.textContainerInset;
textViewHeight.constant = size.height + insets.top + insets.bottom;
It's also important to note, that this method must be called after the UITextView has been rendered. In other words, I originally was resizing this in the viewDidLoad
method of the controller, when in actuality it had to be in viewDidAppear
. This is annoying since the view is already displayed, but I solved it by fading in the few for better aesthetics.
viewDidLayoutSubviews()
in my viewcontroller and call the method it works flawlessly! previously I called the method inviewDidLoad
and wonder why it wasn't working! – Madancy