Skip to content

UITextView height dynamic according to text length, while typing or with already typed data.

Notifications You must be signed in to change notification settings

MallikarjunH/UITextView-Dynamic-Height

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 

Repository files navigation

UITextView-Height-Dynamic

UITextView height dynamic according to text length, while typing or with already typed data.

Following 2 methods are important to achieve this.

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

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

Add textField delegate

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

About

UITextView height dynamic according to text length, while typing or with already typed data.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages