An auto-layout base UITextView subclass which automatically grows with user input and can be constrained by maximal and minimal height - all without a single line of code

Overview

Deprecated

This library is no longer maintained and is deprecated. The repository might be removed at any point in the future.

MBAutoGrowingTextView

An auto-layout based light-weight UITextView subclass which automatically grows and shrinks based on the size of user input and can be constrained by maximal and minimal height - all without a single line of code.

Made primarily for use in Interface builder and only works with Auto layout.

Quick background

Auto layout is a great way to design your interfaces in a modern iOS application. Certain functionality, however, sometimes can't be achived out-of-the-box. One such example is auto-growing/shrinking UITextView. The problem of this view is that is by default a scrollable view, which means that it's intristic size would not change based on the content. One way around it would be to simply make it unscrollable, however this prevents user to scroll up and down when such element reaches it's maximal height. Thus, MBAutoGrowingTextView was created to work around this problem.

Functionality

  • Made for iOS 7, fully leveraging Interface builder and Auto layout.
  • UITextView's height will automatically grow or shrink based on amount of text entered by user
  • Maximal and minimal height of the UITextView can be defined from the interface builder
  • When maximal height is reached, UITextView content becomes scrollable
  • Not a single line of code required for complete functionality

Animated demo

Installation

Installation should be done via Cocoa Pods.

Install CocoaPods if you didn't yet:

sudo gem install cocoapods
pod setup

Change to the directory of your Xcode project, and Create and Edit your Podfile and add following line

pod 'MBAutoGrowingTextView', '~> 0.1.0'
pod install

Open your project in Xcode from the .xcworkspace file (not the usual project file)

Usage

Usage is extremly simple and intuitive if you know your way around Auto Layout. This class leverages Auto-Layout constraints, by changing "height" constraint when needed. It will detect any "height smaller than" or "height greater than" constraints and make sure they are considered when making calculations.

  1. Add UITextView to your interface file (storyboard or xib) and resize it to desired size. Step 1

  2. Assign custom class to it (click the UITextView, open the right Utilities panel, select third tab (Identity inspector) and write MBAutoGrowingTextView into Custom class field). Step 2

  3. Define the auto-layout constraints in your interface builder, as you would normally do. Step 3

  4. To limit maximal height, add the height constant again (as you did above). Select newly added constant and change the Relation field to "Less than or equal". Step 4

  5. To limit minimal height, repeat step 4 and select "Greater than or equal" in relation field

Comments
  • Infinite loop in layoutSubview

    Infinite loop in layoutSubview

    As seems to be fairly common when subclassing UITextView on iOS 7.1, I'm hitting an infinite loop in layoutSubviews when using this pod with the view having scrolling enabled. With scrolling disabled it doesn't work to resize but otherwise doesn't cause an infinite loop.

    opened by jshier 9
  • Add screenshot to the Podspec

    Add screenshot to the Podspec

    Hi, it would be great if you could add a screenshot to the podspec and push the update to the specs repo. The screenshot is currently displayed on http://feeds.cocoapods.org and in future it will displayed on the search of http://cocoapods.org.

    To learn more about the screenshot attribute, you can check the documentation here: http://guides.cocoapods.org/syntax/podspec.html#screenshots

    opened by fabiopelosin 3
  • Correct the spelling of CocoaPods in README

    Correct the spelling of CocoaPods in README

    This pull requests corrects the spelling of CocoaPods 🤓 https://github.com/CocoaPods/shared_resources/tree/master/media

    Created with cocoapods-readme.

    opened by ReadmeCritic 1
  • How would the constraint examples be done programmatically?

    How would the constraint examples be done programmatically?

    How would the constraints from the examples be replicated programmatically?

    My MBAutoGrowingTextView is inside of a scrollview and here is what I tried

    //the horizontal positioning of the textView [_internalScrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_textView(scrollViewSize)]|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:metrics views:elementDict]];

    //a constraint I added that is intended to make the textview expand downward, instead of upward (was able to make this work in the example project with Interface Builder, but doesn't work in my app programmatically [_internalScrollView addConstraint:[NSLayoutConstraint constraintWithItem:_textView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_internalScrollView attribute:NSLayoutAttributeTop multiplier:1.0f constant:1.0]];

    //the three sizing constraints [_internalScrollView addConstraint:[NSLayoutConstraint constraintWithItem:_textView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:33]];

        [_internalScrollView addConstraint:[NSLayoutConstraint constraintWithItem:_textView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:33]];
    
        [_internalScrollView addConstraint:[NSLayoutConstraint constraintWithItem:_textView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationLessThanOrEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:200]];
    
    opened by ewhite-dev 1
  • Height should be optional when setting a min height

    Height should be optional when setting a min height

    Right now an assertion will fire if there's no NSLayoutRelationEqual relation on a height constraint, even if there's a NSLayoutRelationGreaterThanOrEqual relation in place. It seems unnecessary to have both, as the equal relation could be created on the fly. I was stuck for a little while until I realized that it was a requirement. At the very least it should be made clearer in the documentation, as explicit height constraints aren't always required for a fully constrained layout.

    opened by jshier 1
Releases(0.1.0)
Owner
Matej Balantič
Matej Balantič
ARAutocompleteTextView is a subclass of UITextView that automatically displays text suggestions in real-time

ARAutocompleteTextView is a subclass of UITextView that automatically displays text suggestions in real-time. This is perfect for automatically suggesting the domain as a user types an email address, #hashtag or @alexruperez.

Alex Rupérez 261 Jun 29, 2022
VKPinCodeView is simple and elegant UI component for input PIN. You can easily customise appearance and get auto fill (OTP) iOS 12 feature right from the box.

Features Variable PIN length Underline, border and custom styles The error status with / without shake animation Resetting the error status manually,

Vladimir Kokhanevich 95 Nov 24, 2022
A light-weight UITextView subclass that adds support for placeholder.

RSKPlaceholderTextView A light-weight UITextView subclass that adds support for placeholder. Installation Using Swift Package Manager To add the RSKPl

Ruslan Skorb 220 Dec 17, 2022
A UITextView subclass that adds support for multiline placeholder written in Swift.

KMPlaceholderTextView A UITextView subclass that adds support for multiline placeholder written in Swift. Usage You can set the value of the placehold

Zhouqi Mo 795 Nov 18, 2022
An UITextView in Swift. Support auto growing, placeholder and length limit.

GrowingTextView Requirements iOS 8.0 or above Installation CocoaPods GrowingTextView is available through CocoaPods. To install it, simply add the fol

Kenneth Tsang 941 Jan 5, 2023
An auto growing text input bar for messaging apps.

ALTextInputBar An auto growing text input bar for messaging apps. Written in Swift. ALTextInputBar is designed to solve a few issues that folks usuall

Alex Littlejohn 265 Nov 15, 2022
Provides a SwiftUI multi-line TextView implementation including support for auto-sizing. (iOS)

TextView Also available as a part of my SwiftUI+ Collection – just add it to Xcode 13+ Provides a SwiftUI multi-line TextView implementation with supp

SwiftUI+ 51 Jan 3, 2023
VMaskTextField is a library which create an input mask for iOS.

VMaskTextField An inputmask helps the user with the input by ensuring a predefined format. This can be useful for dates, numerics, phone numbers etc U

Vinícius Oliveira 384 Jul 20, 2022
This project will add a done button on your UITextField and UITextView

This project will add a done button on your UITextField and UITextView

Botla Venkatesh 0 Nov 23, 2021
UITextField and UITextView subclasses with placeholders that change into floating labels when the fields are populated with text.

Deprecated Please use JVFloatLabeledTextField instead or feel free to chime in on an issue if you'd like to take over the repo. RPFloatingPlaceholders

rob phillips 1.1k Jan 5, 2023
Animated UITextField and UITextView replacement for iOS

AnimatedTextInput iOS custom text input component used in the Jobandtalent app. Installation Use cocoapods to install this custom control in your proj

jobandtalent 757 Dec 15, 2022
DGPlaceholderTextView - A light-weight UITextView that supports for placeholder

DGPlaceholderTextView Requirements Installation Usage Properties DGPlaceholderTe

donggyu 5 Jan 26, 2022
TTextField is developed to help developers can initiate a fully standard textfield including title, placeholder and error message in fast and convinient way without having to write many lines of codes

TTextField is developed to help developers can initiate a fully standard textfield including title, placeholder and error message in fast and convinient way without having to write many lines of codes

Nguyen Duc Thinh 7 Aug 28, 2022
An UITextField subclass to simplify country code's picking. Swift 5.0

Preview Installation NKVPhonePicker is available through CocoaPods. To install it, simply add the following line to your Podfile: pod 'NKVPhonePicker'

Nike Kov 140 Nov 23, 2022
Handles some of the base configuration to make creating a UIAlertController with text entry even easier. Plus validation!

?? FancyTextEntryController A simpler/easier API for adding text fields to UIAlertControllers. Not a custom view, just uses good ol' UIAlertController

Christian Selig 22 Jul 18, 2022
UITextField that automatically formats text to display in the currency format

CurrencyTextField The numbers that the user enters in the field are automatically formatted to display in the dollar amount format. For example, if th

Richa Deshmukh 49 Sep 28, 2022
A Float Input View with smooth animation and supporting icon and seperator written with Swift

RSFloatInputView Features Smooth animation using CoreText Support optional left icon Support optional seperator Configurable padding, size, fonts and

null 103 Nov 13, 2022
A simple and easily customizable InputAccessoryView for making powerful input bars with autocomplete and attachments

InputBarAccessoryView Features Autocomplete text with @mention, #hashtag or any other prefix A self-sizing UITextView with an optional fixed height (c

Nathan Tannar 968 Jan 2, 2023
UITextField-based control for (NS)Measurement values input.

MeasurementTextField UITextField-based control for (NS)Measurement values input. Provides type-safe keyboard and picker based input of different measu

Siarhei Fiedartsou 16 Jul 22, 2021