Skip to content

carmelogallo/CGMessageTextView

Repository files navigation

CGMessageTextView

A UITextView subclass that adds support for changing dynamically the height in base of the text. Also it has multiline placeholder thanks to https://github.com/MoZhouqi/KMPlaceholderTextView. All is written in Swift and it use AutoLayout.

Usage

You can set one of the following values:

placeholderText
placeholderTextColor
placeholderFont
placeholderTextAlignment

In order to change dinamically the heigh, use one of the follow delegates:

func contentSizeDidChange(withContentSize contentSize: CGSize)
func contentSizeWidthDidChange(withWidth width: CGFloat)
func contentSizeDeltaWidthDidChange(withDelta delta: CGFloat)
func contentSizeHeightDidChange(withHeight height: CGFloat)
func contentSizeDeltaHeightDidChange(withDelta delta: CGFloat)

If you want to know if the text changed, just use the follow delegate

func textDidChange(withText text: String)

Code

Initialization

let messageTextView = CGMessageTextView()
messageTextView.messageTextViewDelegate = self
messageTextView.placeholderText = "Write something..."
addSubview(messageTextView)

Delegates ( all are optional )

extension MyClass: CGMessageTextViewDelegate {
    func contentSizeDidChange(withContentSize contentSize: CGSize) { }
    func contentSizeWidthDidChange(withWidth width: CGFloat) { }
    func contentSizeDeltaWidthDidChange(withDelta delta: CGFloat) { }
    func contentSizeHeightDidChange(withHeight height: CGFloat) { }
    func contentSizeDeltaHeightDidChange(withDelta delta: CGFloat) { }
}

Installation

CocoaPods

You can install the latest release version of CocoaPods with the following command:

$ gem install cocoapods

Simply add the following line to your Podfile:

platform :ios, '10.1'
use_frameworks!

target '<Your Target Name>' do
pod 'CGMessageTextView', '~> 0.3.1'
end

Then, run the following command:

$ pod install

Carthage

Carthage is a decentralized dependency manager that automates the process of adding frameworks to your Cocoa application.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate CGMessageTextView into your Xcode project using Carthage, specify it in your Cartfile:

github "cikpis/CGMessageTextView" ~> 0.3.1

Requirements

  • iOS 10.1+
  • Xcode 10+
  • Swift 4.2+

License

CGMessageTextView is released under the MIT license. See LICENSE for details.