Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

get UITextView to grow with line breaks

I'd like both the dock that the textView sits in and the textView to grow up one line each time I press return for a limit of two lines.

So one press makes the pair grow up one line, same with the second press for a grow total of 2 lines and that's it. Further presses just make the previous text move up inside the textView.

I want it to look like this in the Line chat app.

http://i.stack.imgur.com/YnkfT.jpg

http://i.stack.imgur.com/CLoUm.jpg

http://i.stack.imgur.com/j02QX.jpg

http://i.stack.imgur.com/N0LVj.jpg

I have this:

    @IBOutlet weak var heightConstraint: NSLayoutConstraint!

    func textViewDidChange(messageTextView: UITextView) {

    var sizeThatShouldFitTheContent: CGSize = messageTextView.sizeThatFits(messageTextView.frame.size)
    let heightConstraint = sizeThatShouldFitTheContent.height
    var fixedWidth: CGFloat = messageTextView.frame.size.width
    var size:CGSize = CGSize(width: fixedWidth,height: CGFloat.max)
    var newSize: CGSize = messageTextView.sizeThatFits(CGSizeMake(fixedWidth, CGFloat.max))
    var newFrame: CGRect = messageTextView.frame
    newFrame.size = CGSizeMake(CGFloat(fmaxf(Float(newSize.width), Float(fixedWidth))), newSize.height)
    messageTextView.frame = newFrame
    messageTextView.scrollEnabled = true
    self.messageTextView.sizeToFit()

}