Swift 4: Easy Trick to Auto Sizing UITextView in Real Time

Lets Build That App · 48K views · 2018 · 18:26

Open on YouTube

Software (other)Web developmenttutorialdated

Why Stack Overflow recommends it

Stack Overflow users link it when answering or asking how to make a UITextView grow with its text, including inside table or collection view cells.

Recommended by 1 people across 1 different months, 2018 to 2021.

What it is

A Swift 4 iOS tutorial from the Lets Build That App channel showing how to make a UITextView resize itself automatically as the user types. It walks through a simple approach using Auto Layout and sizeThatFits rather than the more complicated techniques people often use.

What people said

Recommends Fabio Magarelli · 2018-07 · Stack Overflow · on “Resize TextView Heigh AND Width according to text - Swift”

I solved using this function: func adjustUITextViewHeight(arg : UITextView, viewBox : UIView) { arg.isScrollEnabled = false let size = CGSize(width: viewBox.frame.width, height: .infinity) let estimatedSize = arg.sizeThatFits(size) arg.constraints.forEach { (constraint) in if constraint.firstAttribute == .height { constraint.constant = estimatedSize.height } } } Here is the link to the video-tutorial: [this video]

Cites swiftIos · 2018-10 · Stack Overflow · on “Auto-size view with dynamic font in enclosed textview”

This perfect way to do it the content comes from : [this video] . class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // let's create our text view let textView = UITextView() textView.frame = CGRect(x: 0, y: 0, width: 200, height: 100) textView.backgroundColor = .lightGray textView.text = "Here is some default text that we want to show and it might be a couple of lines that are word wrapped" view.addSubview(textView) // use auto layout to set my textview frame...kinda textView.translatesAutoresizingMaskIntoConstraints = false [ textView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor), textView.leadingAnchor.constraint(equalTo: view.l

Cites max123 · 2021-05 · Stack Overflow · on “How do you expand a uicollectionviewcell based on the size of the textview in real time using Swift?”

So what I have is a collection view with a uitextview in each cell. The cells start off with a preset size (for example: a size of 60) and as you type in the textview, the textview grows and the cell grows with it in real time. How would I go about it? I saw this video: [this video] however, he doesn't use a uicollectionviewcell. Is there a method to use where the cell doesn't have a fixed width and grows with the contents inside?