UITextView
has extra paddings by default. Sometimes it's annoying especially if you want to measure some text without view instance and place them at some area precisely.
Do this to remove such paddings.
messageTextView.textContainerInset = UIEdgeInsetsZero
messageTextView.textContainer.lineFragmentPadding = 0
Now you can measure text size using NSAttributedString.boundingRectWithSize(...)
, and resize a UITextView
just to fit it to the text.
let budget = getSomeCGSizeBudget()
let text = getSomeAttributedString()
let textSize = text.boundingRectWithSize(budget, options: [.UsesLineFragmentOrigin, .UsesFontLeading], context: nil).size
messageTextView.frame.size = textSize // Just fits.