Resize a textView based on it's content iOS || textView fit its content objective C || textView resize based on content swift

Solution:

In iOS textVIew will be scroll when text overflows its frame.in android it had an option that wrap content.So we can use the below technique to achieve that.

Objective c:


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


Swift:


    func textViewDidChange(_ textView: UITextView) {

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

    }

Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

store cgpoint in userdefaults iOS swift