Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Chaneg Textview height according to text

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 6.07k
    Comment on it

    Hi All,

    If you want to change height of textView according to content height than you can follow this code.

    Use auto layout to set view in story board and make IBOutlet for textview . Assign delegate to textview and dont give height constraint to textview

    In Objective c

    Add this delegate method in your view controller.

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

    In Swift

    Add this delegate method in your viewcontroller

    func textViewDidChange(textView: UITextView){
            let fixedWidth = textView.frame.size.width
            textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.max))
            let newSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.max))
            var newFrame = textView.frame
            newFrame.size = CGSize(width: max(newSize.width, fixedWidth), height: newSize.height)
            textView.frame = newFrame;
        }
    

    Now textview height will change according to text in it.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: