Skip to content

Latest commit

 

History

History
91 lines (66 loc) · 2.54 KB

README.md

File metadata and controls

91 lines (66 loc) · 2.54 KB

GrowingTextView

Version License Platform Language

Requirements

iOS 8.0 or above

Installation

GrowingTextView is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "GrowingTextView"

or

Copy GrowingTextView.swift into your project.

Usage

let textView = GrowingTextView()
addSubview(textView)

Customization

Parameter Type Description Default
maxLength Int Maximum text length. Exceeded text will be trimmed. 0 means no limit. 0
trimWhiteSpaceWhenEndEditing Bool Trim white space and new line characters when textview did end editing. true
placeHolder String? PlaceHolder text. nil
placeHolderColor UIColor? PlaceHolder text color. nil
placeHolderLeftMargin CGFloat Left margin of PlaceHolder text. 5.0
maxHeight CGFloat Maximum height of textview. 0.0

Examples

textView.maxLength = 140
textView.trimWhiteSpaceWhenEndEditing = false
textView.placeHolder = "Say something..."
textView.placeHolderColor = UIColor(white: 0.8, alpha: 1.0)
textView.maxHeight = 70.0
textView.backgroundColor = UIColor.whiteColor()
textView.layer.cornerRadius = 4.0

Animation

To animate the height change, adopt GrowingTextViewDelegate protocol instead of UITextViewDelegate.

class ViewController: UIViewController, GrowingTextViewDelegate {
...

Implement textViewDidChangeHeight. Call layoutIfNeeded() inside an animation.

func textViewDidChangeHeight(height: CGFloat) {
   UIView.animateWithDuration(0.2) { () -> Void in
       self.textView.layoutIfNeeded()
   }
}

In some cases, you may also need to animate it's superview, e.g. toolbar.

func textViewDidChangeHeight(height: CGFloat) {
   UIView.animateWithDuration(0.2) { () -> Void in
       self.myToolbar.layoutIfNeeded()
   }
}

Author

Kenneth Tsang, [email protected]

License

GrowingTextView is available under the MIT license. See the LICENSE file for more info.