Latest 0.6.0
Homepage https://github.com/alexsteinerde/ASGrowingTextView
License MIT
Platforms ios 8.0, requires ARC
Authors ,

GrowingTextView

This is a fork of https://github.com/KennethTsang/GrowingTextView.
Version
License
Platform
Language
Language

Requirements

iOS 8.0 or above

Installation

CocoaPods

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

Swift 4

pod 'GrowingTextView', '~> 0.6'

Manually

Copy GrowingTextView.swift into your project.

Usage

Example

GrowingTextview is just a textview, download the example to see how to use it as a input toolbar like other instant messaging apps.

Using GrowingTextview programmatically

let textView = GrowingTextView()
textView.delegate = self
addSubview(textView)

Using GrowingTextview in Storyboard

  1. Drag a TextView into Storyboard.
  2. Set class to "GrowingTextView".
  3. Set delegate to it’s view controller.
automaticallyAdjustsScrollViewInsets

Sometime the view controller may incorrectly adjust the inset of textview automatically. To avoid this, set automaticallyAdjustsScrollViewInsets to false

override func viewDidLoad() {
    super.viewDidLoad()
    automaticallyAdjustsScrollViewInsets = false
}

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. UIColor(white: 0.8, alpha: 1.0)
attributedPlaceHolder NSAttributedString? Attributed PlaceHolder text. nil
minHeight CGFloat Minimum height of textview. 0.0
maxHeight CGFloat Maximum height of textview. 0.0
parentScrollView UIScrollView? Parent scroll view that will be scrolled to the correct position when neccesary. nil

Examples

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

Animation

  1. Adopt GrowingTextViewDelegate instead of UITextViewDelegate.
  2. Implement textViewDidChangeHeight.
  3. Call layoutIfNeeded() on superview inside the animation.
class ViewController: UIViewController, GrowingTextViewDelegate {
    func textViewDidChangeHeight(_ textView: GrowingTextView, height: CGFloat) {
       UIView.animate(withDuration: 0.2) {
           self.view.layoutIfNeeded()
       }
    }
}

TableView

If the text view is a subview of a table view and the parent scroll view is this table view it is necessary to update the table view:

class ViewController: UIViewController, GrowingTextViewDelegate {
    func textViewDidChangeHeight(_ textView: GrowingTextView, height: CGFloat) {
        DispatchQueue.main.async {
            UIView.setAnimationsEnabled(false)
            tableView.beginUpdates()
            tableView.endUpdates()
            UIView.setAnimationsEnabled(true)
        }
    }
}

Delegate

GrowingTextViewDelegate is inherited from UITextViewDelegate. You may use it’s delegate function as a normal UITextView.

class ViewController: UIViewController, GrowingTextViewDelegate {
    func textViewDidChange(_ textView: UITextView) {
        ...
    }
    func textViewDidEndEditing(_ textView: UITextView) {
        ...
    }
}

Check out UITextViewDelegate here: https://developer.apple.com/reference/uikit/uitextviewdelegate

Author

Origin Author: Kenneth Tsang, [email protected]
Editied small things: Alex Steiner, [email protected]

License

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

Latest podspec

{
    "name": "ASGrowingTextView",
    "version": "0.6.0",
    "summary": "UITextView on Swift 3 and Swift 4. Support auto growing, placeholder and length limit.",
    "description": "This cocoapod provides a subclass of UITextView which support auto growing, placeholder, length limit and parent scrollview support.",
    "homepage": "https://github.com/alexsteinerde/ASGrowingTextView",
    "screenshots": "https://raw.githubusercontent.com/alexsteinerde/ASGrowingTextView/master/DEMO.gif",
    "license": "MIT",
    "authors": {
        "Kenneth Tsang": "[email protected]",
        "Alex Steiner": "[email protected]"
    },
    "source": {
        "git": "https://github.com/alexsteinerde/ASGrowingTextView.git",
        "tag": "0.6.0"
    },
    "platforms": {
        "ios": "8.0"
    },
    "requires_arc": true,
    "source_files": "Pod/Classes/**/*",
    "pushed_with_swift_version": "4.0"
}

Pin It on Pinterest

Share This