Self Sizing Uitextview Till Specific Height

Self sizing uitextview till specific height

Disable Scrolling of textView.

Sample Image

Sample Image

TO Increase Height to a specific value and then enable scrolling.

Provide a maximum height constraint then add this code to your viewController

 class YourViewController: UIViewController, UITextViewDelegate
{
@IBOutlet weak var yourTextView: UITextView!

let textViewMaxHeight: CGFloat = 100
override func viewDidLoad()
{
super.viewDidLoad()
yourTextView.delegate = self
}

func textViewDidChange(textView: UITextView)
{
if textView.contentSize.height >= self.textViewMaxHeight
{
textView.scrollEnabled = true
}
else
{
textView.frame.size.height = textView.contentSize.height
textView.scrollEnabled = false
}
}
}

how to make UITextView height dynamic according to text length?

this Works for me, all other solutions didn't.

func adjustUITextViewHeight(arg : UITextView) {
arg.translatesAutoresizingMaskIntoConstraints = true
arg.sizeToFit()
arg.scrollEnabled = false
}

In Swift 4 the syntax of arg.scrollEnabled = false has changed to arg.isScrollEnabled = false.

How can I prevent a self-sizing UITextView from growing past a specific point (keyboard top anchor, e.g.)?

A simple solution is to give a height-constraint to the textView. Set isScrollEnabled property of the textView to false. Allow it to grow until it reaches the keyboard origin(you have to check this programmatically). Once it reaches the keyboard origin, make the height fixed and set isScrollEnabled property to true

How do I size a UITextView to its content?

This works for both iOS 6.1 and iOS 7:

- (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;
}

Or in Swift (Works with Swift 4.1 in iOS 11)

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)

If you want support for iOS 6.1 then you should also:

textview.scrollEnabled = NO;

Get UITextView dynamic height with auto layout after setting text

This should work:

NSLog(@"text before: %.2f",self.myText.frame.size.height);
[self.myText setText:self.string];
[self.myText layoutIfNeeded]; // <--- Add this
NSLog(@"text after: %.2f",self.myText.frame.size.height);

Here's an example implementation on my Github: https://github.com/guillaume-algis/SO-27060338

UITextView Dynamic Height Using Autolayout - Top line of text cutoff with each new line

Oh, sorry for necroposting, but I had the same problem and finally found a solution.

So basicly u neeed:

if textView.contentSize.height <= textView.height {
textView.scrollRangeToVisible(NSMakeRange(0, 0))
}

It will scroll text to correct position if u didn't reached maximum toolbar height. If u reached it, than ur text view content size is higher than its height and u shouldn't have this problem.

Edit: Credits to
https://stackoverflow.com/a/19047464/1316040 for mentioning HPGrowingTextView.
https://github.com/HansPinckaers/GrowingTextView for... well, existing
https://github.com/KennethTsang/GrowingTextView for actual line of code scrollRangeToVisible

Swift: autoresizing the height UITextView inside a cell

You are able to use Auto Layout to achieve self-sizing table view cells with dynamic height.

First, allow the table view to use the Auto Layout constraints and the contents of its cells to determine each cell’s height, by setting the following:

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 100 // Something close to your average cell height

Next, go to your storyboard and ensure that scrolling is disabled for your text view. This is an important setting to tell the textview to size itself accordingly based on its contents.

Leave out the height constraint for the textview because you want it to grow dynamically.

Finally, ensure that you have pinned the top of the textview to the bottom of the top label, and the bottom of the textview to the top of the bottom label. These labels should themselves be pinned to the top and bottom of the cell respectively, so that the cell can determine its total height from top to bottom.

You may also likely have to tweak the vertical compression resistance and the vertical hugging priority of the textview, to give Auto Layout a hint as to which view it should prioritise to compress or expand to fill up the cell space (Auto Layout should warn you on this in the storyboard).

How to change the TextView height dynamically to a threshold and then allow scrolling?

It seems your code requires two changes, and it will work fine.

  1. Instead of min height of constraint provide max height of 100:

Height Constraint for TextView


  1. Change code as below:

     import UIKit

    class ViewController: UIViewController, UITextViewDelegate
    {
    @IBOutlet weak var messageTextView: UITextView!

    let messageTextViewMaxHeight: CGFloat = 100
    override func viewDidLoad()
    {
    super.viewDidLoad()
    messageTextView.delegate = self
    }

    func textViewDidChange(textView: UITextView)
    {
    if textView.contentSize.height >= self.messageTextViewMaxHeight
    {
    textView.scrollEnabled = true
    }
    else
    {
    textView.frame.size.height = textView.contentSize.height
    textView.scrollEnabled = false // textView.isScrollEnabled = false for swift 4.0
    }
    }
    }

UITextView that expands to text using auto layout

The view containing UITextView will be assigned its size with setBounds by AutoLayout. So, this is what I did. The superview is initially set up all the other constraints as they should be, and in the end I put one special constraint for UITextView's height, and I saved it in an instance variable.

_descriptionHeightConstraint = [NSLayoutConstraint constraintWithItem:_descriptionTextView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:0.f
constant:100];

[self addConstraint:_descriptionHeightConstraint];

In the setBounds method, I then changed the value of the constant.

-(void) setBounds:(CGRect)bounds
{
[super setBounds:bounds];

_descriptionTextView.frame = bounds;
CGSize descriptionSize = _descriptionTextView.contentSize;

[_descriptionHeightConstraint setConstant:descriptionSize.height];

[self layoutIfNeeded];
}


Related Topics



Leave a reply



Submit