How to Adjust the Size of a UITextView to Fit Its Content

How to Adjust the Size of a UITextView to Fit Its Content

6 June 2024 Stephan Petzl Leave a comment Tech-Help

When working with UITextView in iOS development, you may encounter situations where you need the text view to dynamically adjust its size based on the content it holds. This is particularly useful in scenarios such as messaging apps or note-taking applications where the text content can vary significantly. In this guide, we will explore the best practices for resizing a UITextView to fit its content.

Solution Using sizeThatFits Method

The most reliable method for resizing a UITextView is to use the sizeThatFits method. This approach works for both Objective-C and Swift, and is compatible with iOS versions starting from iOS 6.1.

Objective-C Implementation


- (void)textViewDidChange:(UITextView *)textView {
    CGFloat fixedWidth = textView.frame.size.width;
    CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
    CGRect newFrame = textView.frame;
    newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
    textView.frame = newFrame;
}
    

Swift Implementation


let fixedWidth = textView.frame.size.width
let newSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude))
textView.frame.size = CGSize(width: max(newSize.width, fixedWidth), height: newSize.height)
    

In addition, for iOS 6.1 support, you should set textView.scrollEnabled to false:

textView.scrollEnabled = false

Alternative Method Using contentSize

Another method to resize a UITextView is by using its contentSize property. However, this method is not recommended for iOS 7 or later versions due to compatibility issues. For those versions, it’s better to use the sizeThatFits method along with Auto Layout constraints.

Example with Auto Layout


CGSize sizeThatShouldFitTheContent = textView.sizeThatFits(textView.frame.size)
heightConstraint.constant = sizeThatShouldFitTheContent.height
    

Practical Example

Consider a scenario where you have a UITextView inside a UITableViewCell. To dynamically adjust the size of the UITextView based on its content, you can follow these steps:

  1. Add a UITextView to a UITableViewCell and constrain it to the sides.
  2. Set the UITextView’s scrollEnabled property to NO.
  3. In your viewDidLoad method, enable automatic row height calculation:

tableView.estimatedRowHeight = 150
tableView.rowHeight = UITableViewAutomaticDimension
    

For editable UITextView, implement the textViewDidChange method:


- (void)textViewDidChange:(UITextView *)textView {
    [tableView beginUpdates];
    [tableView endUpdates];
}
    

Leveraging Repeato for Automated Testing

Ensuring that your UITextView resizes correctly can be a repetitive and time-consuming task. This is where automated testing tools like Repeato come into play. Repeato is a no-code test automation tool for iOS and Android that allows you to create, run, and maintain automated tests for your apps. It uses computer vision and AI for fast and easy test creation and maintenance.

With Repeato, mobile developers can focus on creating great products instead of spending time on testing. The tool is particularly useful for verifying UI changes such as the dynamic resizing of UITextView. Additionally, Repeato allows non-technical colleagues or QA engineers to handle test automation, making the process more efficient.

For more information on automated testing and how Repeato can help streamline your development workflow, visit our documentation page or contact us.

Like this article? there’s more where that came from!