Skip to content

Commit

Permalink
use GrowingTextViewHandler in a table view cell
Browse files Browse the repository at this point in the history
  • Loading branch information
hsusmita committed Aug 8, 2016
1 parent cd75f89 commit dcab8d7
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
objects = {

/* Begin PBXBuildFile section */
4EF6FE661D586146003863CC /* FormViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4EF6FE651D586146003863CC /* FormViewController.swift */; };
4EF6FE681D586229003863CC /* FormTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4EF6FE671D586229003863CC /* FormTableViewCell.swift */; };
69CEFFCB1A9D9F03003B4786 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 69CEFFCA1A9D9F03003B4786 /* AppDelegate.swift */; };
69CEFFCD1A9D9F03003B4786 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 69CEFFCC1A9D9F03003B4786 /* ViewController.swift */; };
69CEFFD01A9D9F03003B4786 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 69CEFFCE1A9D9F03003B4786 /* Main.storyboard */; };
Expand All @@ -27,6 +29,8 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
4EF6FE651D586146003863CC /* FormViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FormViewController.swift; sourceTree = "<group>"; };
4EF6FE671D586229003863CC /* FormTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FormTableViewCell.swift; sourceTree = "<group>"; };
69CEFFC51A9D9F03003B4786 /* GrowingTextViewHandler.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = GrowingTextViewHandler.app; sourceTree = BUILT_PRODUCTS_DIR; };
69CEFFC91A9D9F03003B4786 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
69CEFFCA1A9D9F03003B4786 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -86,6 +90,8 @@
69CEFFD11A9D9F03003B4786 /* Images.xcassets */,
69CEFFD31A9D9F03003B4786 /* LaunchScreen.xib */,
69CEFFC81A9D9F03003B4786 /* Supporting Files */,
4EF6FE671D586229003863CC /* FormTableViewCell.swift */,
4EF6FE651D586146003863CC /* FormViewController.swift */,
);
path = GrowingTextViewHandler;
sourceTree = "<group>";
Expand Down Expand Up @@ -227,6 +233,8 @@
files = (
69CEFFEC1A9D9FB1003B4786 /* GrowingTextViewHandler.swift in Sources */,
69CEFFCD1A9D9F03003B4786 /* ViewController.swift in Sources */,
4EF6FE661D586146003863CC /* FormViewController.swift in Sources */,
4EF6FE681D586229003863CC /* FormTableViewCell.swift in Sources */,
69CEFFCB1A9D9F03003B4786 /* AppDelegate.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6254" systemVersion="13F34" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="vXZ-lx-hvc">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="10116" systemVersion="15F34" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="owB-Xf-cbh">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6247"/>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="10085"/>
</dependencies>
<scenes>
<!--View Controller-->
Expand All @@ -28,13 +29,22 @@
<outlet property="delegate" destination="vXZ-lx-hvc" id="rI6-YI-qWf"/>
</connections>
</textView>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="bW7-U6-WNd">
<rect key="frame" x="234" y="79" width="132" height="30"/>
<state key="normal" title="TableView Example"/>
<connections>
<segue destination="9Tt-QS-DYI" kind="show" id="iNQ-Be-kT4"/>
</connections>
</button>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
<constraints>
<constraint firstItem="q2N-J0-Gt2" firstAttribute="top" secondItem="jyV-Pf-zRb" secondAttribute="bottom" constant="111" id="093-rG-cLX"/>
<constraint firstItem="q2N-J0-Gt2" firstAttribute="leading" secondItem="kh9-bI-dsS" secondAttribute="leading" constant="100" id="3G5-un-awm"/>
<constraint firstItem="bW7-U6-WNd" firstAttribute="centerX" secondItem="kh9-bI-dsS" secondAttribute="centerX" id="51b-Tb-2Qb"/>
<constraint firstItem="2fi-mo-0CV" firstAttribute="top" secondItem="q2N-J0-Gt2" secondAttribute="bottom" constant="439" id="8kE-3h-jvN"/>
<constraint firstAttribute="centerY" secondItem="q2N-J0-Gt2" secondAttribute="centerY" constant="154" id="d2s-Ko-e3j"/>
<constraint firstItem="bW7-U6-WNd" firstAttribute="top" secondItem="jyV-Pf-zRb" secondAttribute="bottom" constant="15" id="mco-ua-6jX"/>
<constraint firstAttribute="trailing" secondItem="q2N-J0-Gt2" secondAttribute="trailing" constant="100" id="vtf-8N-2AS"/>
</constraints>
<variation key="default">
Expand All @@ -44,13 +54,104 @@
</mask>
</variation>
</view>
<navigationItem key="navigationItem" id="h8V-m7-c7M"/>
<connections>
<outlet property="heightConstraint" destination="3a3-GU-7qL" id="tj1-eg-xi3"/>
<outlet property="textView" destination="q2N-J0-Gt2" id="aDg-Gw-uVg"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="x5A-6p-PRh" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="1357" y="425"/>
</scene>
<!--Form View Controller-->
<scene sceneID="mv7-0g-QYk">
<objects>
<viewController automaticallyAdjustsScrollViewInsets="NO" id="9Tt-QS-DYI" customClass="FormViewController" customModule="GrowingTextViewHandler" customModuleProvider="target" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="Gau-Ra-yMM"/>
<viewControllerLayoutGuide type="bottom" id="bXl-cO-cPl"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="31c-6h-GyD">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="singleLineEtched" rowHeight="44" sectionHeaderHeight="28" sectionFooterHeight="28" translatesAutoresizingMaskIntoConstraints="NO" id="BDi-64-MBC">
<rect key="frame" x="0.0" y="64" width="600" height="536"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<prototypes>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="blue" hidesAccessoryWhenEditing="NO" indentationLevel="1" indentationWidth="0.0" reuseIdentifier="FormTableViewCellIdentifier" id="TGc-us-FUl" customClass="FormTableViewCell" customModule="GrowingTextViewHandler" customModuleProvider="target">
<rect key="frame" x="0.0" y="28" width="600" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="TGc-us-FUl" id="AgD-cy-1Vb">
<rect key="frame" x="0.0" y="0.0" width="600" height="43.5"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<textView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" textAlignment="natural" translatesAutoresizingMaskIntoConstraints="NO" id="sIY-p6-tJe">
<rect key="frame" x="5" y="5" width="590" height="50"/>
<color key="backgroundColor" red="0.76862745099999996" green="0.76862745099999996" blue="0.78039215689999997" alpha="1" colorSpace="calibratedRGB"/>
<constraints>
<constraint firstAttribute="height" constant="50" id="mFB-Y8-03g"/>
</constraints>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits" autocapitalizationType="sentences"/>
<connections>
<outlet property="delegate" destination="TGc-us-FUl" id="Ymg-h4-YgU"/>
</connections>
</textView>
</subviews>
<color key="backgroundColor" red="0.0" green="0.47843137250000001" blue="1" alpha="1" colorSpace="calibratedRGB"/>
<constraints>
<constraint firstAttribute="trailing" secondItem="sIY-p6-tJe" secondAttribute="trailing" constant="5" id="MgV-Bl-ZDW"/>
<constraint firstItem="sIY-p6-tJe" firstAttribute="leading" secondItem="AgD-cy-1Vb" secondAttribute="leading" constant="5" id="dyV-up-yjA"/>
<constraint firstItem="sIY-p6-tJe" firstAttribute="top" secondItem="AgD-cy-1Vb" secondAttribute="top" constant="5" id="n4q-I8-hbt"/>
<constraint firstAttribute="bottom" secondItem="sIY-p6-tJe" secondAttribute="bottom" priority="999" constant="5" id="phL-Ct-cne"/>
</constraints>
</tableViewCellContentView>
<connections>
<outlet property="heightConstraint" destination="mFB-Y8-03g" id="9sz-4A-Iso"/>
<outlet property="textView" destination="sIY-p6-tJe" id="dot-Gt-fWf"/>
</connections>
</tableViewCell>
</prototypes>
<connections>
<outlet property="dataSource" destination="9Tt-QS-DYI" id="4Yf-Ws-Z3x"/>
<outlet property="delegate" destination="9Tt-QS-DYI" id="zu8-4b-N8N"/>
</connections>
</tableView>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstItem="BDi-64-MBC" firstAttribute="top" secondItem="Gau-Ra-yMM" secondAttribute="bottom" id="B2w-Q3-C35"/>
<constraint firstItem="BDi-64-MBC" firstAttribute="leading" secondItem="31c-6h-GyD" secondAttribute="leading" id="OV0-DV-Ffv"/>
<constraint firstAttribute="trailing" secondItem="BDi-64-MBC" secondAttribute="trailing" id="xFA-NG-xE2"/>
<constraint firstItem="bXl-cO-cPl" firstAttribute="top" secondItem="BDi-64-MBC" secondAttribute="bottom" id="yLH-D7-8jc"/>
</constraints>
</view>
<connections>
<outlet property="tableView" destination="BDi-64-MBC" id="elh-C1-92Z"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="bY7-x7-UdX" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="2029" y="425"/>
</scene>
<!--Navigation Controller-->
<scene sceneID="HR1-KH-sNk">
<objects>
<navigationController automaticallyAdjustsScrollViewInsets="NO" id="owB-Xf-cbh" sceneMemberID="viewController">
<toolbarItems/>
<navigationBar key="navigationBar" contentMode="scaleToFill" id="i6N-MV-HCL">
<rect key="frame" x="0.0" y="0.0" width="320" height="44"/>
<autoresizingMask key="autoresizingMask"/>
</navigationBar>
<nil name="viewControllers"/>
<connections>
<segue destination="vXZ-lx-hvc" kind="relationship" relationship="rootViewController" id="ZJ9-NH-A9c"/>
</connections>
</navigationController>
<placeholder placeholderIdentifier="IBFirstResponder" id="q8b-Lf-Agd" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="545" y="425"/>
</scene>
</scenes>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// FormTableViewCell.swift
// GrowingTextViewHandler
//
// Created by Susmita Horrow on 08/08/16.
// Copyright © 2016 hsusmita.com. All rights reserved.
//

import UIKit

protocol FormTableViewCellDelegate {
func formTableViewCell(formTableViewCell: FormTableViewCell, shouldChangeHeight height: CGFloat)
}

class FormTableViewCell: UITableViewCell {
@IBOutlet weak var textView: UITextView!
private var handler: GrowingTextViewHandler?
var delegate: FormTableViewCellDelegate?
@IBOutlet weak var heightConstraint: NSLayoutConstraint!

override func awakeFromNib() {
super.awakeFromNib()
handler = GrowingTextViewHandler(textView: self.textView, heightConstraint: self.heightConstraint)
handler?.minimumNumberOfLines = 2
handler?.maximumNumberOfLines = 6
handler?.setText("", animated: false)
// handler?.setText("Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.", animated: false)
}

override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}

func textViewDidChange(textView: UITextView) {
let oldFrame = textView.frame
self.handler?.resizeTextView(animated:true)
let currentFrame = textView.frame
if oldFrame.height != currentFrame.height {
delegate?.formTableViewCell(self, shouldChangeHeight: textView.frame.height)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// FormViewController.swift
// GrowingTextViewHandler
//
// Created by Susmita Horrow on 08/08/16.
// Copyright © 2016 hsusmita.com. All rights reserved.
//

import UIKit

class FormViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
super.viewDidLoad()
tableView.estimatedRowHeight = 50.0
tableView.rowHeight = UITableViewAutomaticDimension
}
}

extension FormViewController: UITableViewDataSource {
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("FormTableViewCellIdentifier") as? FormTableViewCell
cell?.delegate = self
return cell!
}
}

extension FormViewController: FormTableViewCellDelegate {
func formTableViewCell(formTableViewCell: FormTableViewCell, shouldChangeHeight height: CGFloat) {
tableView.beginUpdates()
tableView.endUpdates()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,14 @@ class ViewController: UIViewController,UITextViewDelegate {
handler = GrowingTextViewHandler(textView: self.textView, heightConstraint: self.heightConstraint)
handler?.minimumNumberOfLines = 2
handler?.maximumNumberOfLines = 6
handler?.setText("", animated: false)
}

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func textViewDidChange(textView: UITextView) {
self.handler?.resizeTextView(animated:true)
}
}

0 comments on commit dcab8d7

Please sign in to comment.