forked from hsusmita/GrowingTextViewHandler
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use GrowingTextViewHandler in a table view cell
- Loading branch information
Showing
5 changed files
with
197 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
GrowingTextViewHandler/GrowingTextViewHandler/FormTableViewCell.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
GrowingTextViewHandler/GrowingTextViewHandler/FormViewController.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters