2

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.

Tom Elliott
  • 1,908
  • 1
  • 19
  • 39
K.K
  • 101
  • 3
  • 9

1 Answers1

6

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 the frame or size does.

import Foundation
import UIKit
import XCPlayground

var str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean feugiat purus a pharetra rhoncus. Phasellus eget velit risus. Duis varius et massa non pretium. Praesent sollicitudin egestas mi, non cursus eros ornare congue. Pellentesque ex leo, hendrerit posuere euismod ut, ultricies eleifend lectus. Phasellus eu interdum sem. Quisque ut condimentum justo. Integer non neque mauris. Vestibulum eu tellus ac nunc mollis maximus a vitae lectus. In hac habitasse platea dictumst. Sed neque sapien, elementum quis volutpat quis, dictum id ligula. Cras egestas finibus fermentum."

let textViewFrame = CGRectZero

let textStorage = NSTextStorage()

let layoutManager = NSLayoutManager()
textStorage.addLayoutManager(layoutManager)

let textContainer = NSTextContainer(size: textViewFrame.size)
layoutManager.addTextContainer(textContainer)

textContainer.widthTracksTextView = true
textContainer.heightTracksTextView = true

var textView = UITextView(frame: textViewFrame, textContainer: textContainer)
XCPShowView("MyView", view: textView)

textView.text = str

//textView.bounds = CGRect(x: 0, y: 0, width: 200, height: 200)
//textView.frame = CGRect(x: 0, y: 0, width: 200, height: 200)
textView.frame.size = CGSize(width: 200, height: 200)

//textView.bounds = CGRect(x: 0, y: 0, width: 400, height: 200)
//textView.frame = CGRect(x: 0, y: 0, width: 400, height: 200)
textView.frame.size = CGSize(width: 400, height: 200)

Future readers, if you are here because you are rotating a UITextView : Transform UITextView

Community
  • 1
  • 1
R Menke
  • 8,183
  • 4
  • 35
  • 63
  • You are right, thanks. But one thing: I was changing bounds instead of frame because textView is rotated with CGAffineTransform. What should I do in this situation? – K.K Oct 03 '15 at 17:22
  • @K.K damn not too familiar with CGAffineTransform. A good place to debug this is a playground. (if your code is pretty modular) You can directly see the effects. So debugging goes way faster. Maybe try this: [good rotating code](https://github.com/FlexMonkey/Rotatable) – R Menke Oct 03 '15 at 17:26
  • @K.K Tried the code I linked. Also gives weird results. Sorry! Will take a closer look! – R Menke Oct 03 '15 at 17:41
  • @K.K [Applying transform to UITextView](http://stackoverflow.com/questions/6479727/applying-transform-to-uitextview-prevent-content-resizing) I found this when googling "rotate UITextView" it will fix your issues. – R Menke Oct 03 '15 at 18:38