After resizing textView, I want textContainer to be the same size (I want text to be the same width as resized textView) as textView. So what I am doing right now is I create a UITextView
programatically, then (with or without text inside textView) I resize textView by changing it's bounds but textContainer stays with the same size (text is in smaller rectangle than textView which looks bad).
Here is some code:
let textStorage = NSTextStorage()
let layoutManager = NSLayoutManager()
textStorage.addLayoutManager(layoutManager)
let textContainer = NSTextContainer()
layoutManager.addTextContainer(textContainer)
textContainer.widthTracksTextView = true
textContainer.heightTracksTextView = true
self.textView = UITextView(frame: textViewFrame, textContainer: textContainer)
And later:
self.textView.bounds = CGRect(x, y, newWidth, newHeight)
Currently I am out of ideas why it's not working and can't find solution.
Throw this in a playground. Comment out/in the different ways to set the size of the textView. Setting the
bounds
indeed doesn't update the container. setting theframe
orsize
does.Future readers, if you are here because you are rotating a UITextView : Transform UITextView