How to set UITextView's height dynamically in UITableViewCell based on string size?
When using Auto-Layout for dynamically sizing cells, you don't really need to implement sizeThatFits(...)
. If the constraints are setup correctly, then you only need to disable the scrolling of the UITextView
.
From code:
yourTextView.scrollEnabled = false
From IB:
Select your Text View and open Attributes inspector, then
Swift / how to use UITextView with dynamic height INSIDE UITableViewCell with dynamic height
Get programmatically height of textview...
let textView = UITextView()//your Text view
let sizeThatShouldFitTheContent = textView.sizeThatFits(textView.frame.size)
let height = sizeThatShouldFitTheContent.height
OR try this demo...
Self-sizing UITextView in a UITableView
https://www.damienpontifex.com/posts/self-sizing-uitableviewcell-with-uitextview-in-ios8/
How can I adjust the UITableViewCell height to the content of UITextView that's inside?
The key to getting self-sizing table cells (autolayout-based, which I recommend) is as follows:
- Add your subviews to the
contentView
of theUITableViewCell
- Provide constraints between your subviews and the
contentView
such that your subviews reach all edges of the table cell. In your case, this probably means aligning theleading
,trailing
,top
, andbottom
edges of yourUITextView
to the corresponding edges of thecontentView
. - Set the row height to
UITableViewAutomaticDimension
instead of a hardcodedCGFloat
. - Somewhere in your controller, provide an estimation of the height with
tableView.estimatedRowHeight = x
(a hard coded constant is fine, this is for performance).
Any way to dynamically adjust the height of a UITextView inside a UIStackView inside a UITableViewCell?
Turns out that just writing out that question in detail and then looking at a different question on StackOverflow allowed me to find one of several answers that described the solution to this predicament.
The answer:
func textViewDidChange(_ textView: UITextView) {
self.listTableView.beginUpdates()
self.listTableView.endUpdates()
}
Calling .beginUpdates()
and .endUpdates()
apparently calls the table view's delegate methods without breaking the editing process like .reloadData()
does, allowing for truly dynamic table row height changes while editing a text view inside the table row.
UITextView and cell dynamically update height based on content of textview?
This works for ios 7 too
this goes into your tableviewcell
protocol HeightForTextView {
func heightOfTextView(height: CGFloat)
}
class TableViewCell: UITableViewCell {
@IBOutlet weak var textView: UITextView!
weak var delgate: HeightForTextView?
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
func textViewDidChange(textView: UITextView) {
var fixedWidth: CGFloat = textView.frame.size.width
var newSize: CGSize = textView.sizeThatFits(CGSizeMake(fixedWidth, CGFloat.max))
if let iuDelegate = self.delgate {
iuDelegate.heightOfTextView(newSize.height)
}
}
}
your tableview controller should be
class TableViewController: UITableViewController,HeightForTextView {
var textViewHeight = CGFloat()
//In your cell for row method set your your delegate
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TableViewCell
cell.delgate = self
return cell
}
//delegate method
func heightOfTextView(height: CGFloat) {
textViewHeight = height
self.tableView.beginUpdates()
self.tableView.endUpdates()
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return textViewHeight + 70
}
}
Set width and height of UITextView based on the dynamic text
Try this code:
Answer 1:
func tableView(tableView:UITableView!, numberOfRowsInSection section: Int) -> Int {
yourTableViewName.estimatedRowHeight = 44.0 // Standard tableViewCell size
yourTableViewName.rowHeight = UITableViewAutomaticDimension
return yourArrayName.count }
And also put this code inside your Cell for incase...
yourCell.sizeToFit()
Answer 2:
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
UITableViewCell, UITextView with dynamic height
Use UILabel
for your cell text. You can then use sizeWithFont:constrainedToSize:
to calculate the height of that UILabel within each cell. For example:
#define PADDING 10.0f
- (CGFloat)tableView:(UITableView *)t heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *text = [self.items objectAtIndex:indexPath.row];
CGSize textSize = [text sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:CGSizeMake(self.tableView.frame.size.width - PADDING * 3, 1000.0f)];
return textSize.height + PADDING * 3;
}
Related Topics
Correct Way to Layout Swiftui (Similar to Autolayout)
How to Schedule a Same Local Notification in Swift
How Do View Types Like Text or Image Conform to the View Protocol in Swiftui
Settitletextattributes Doesn't Work for Uitabbaritem When It Is Unselected in Swift
iOS - Uinavigationcontroller, Hide Navigationbar
Icloud Document Picker from Wkwebview Dismissing Container View
Removing a View Controller from Memory When Instantiating a New View Controller
Uitextfield Doesn't End Editing When Button Clicked( Delegate Textfielddidendediting )
Skspritenode Subclassing Swift
Infinite Scroll on iOS with Swift
Wake Up Application in Background Using Audiosession Like Alarmy iOS App
Double Tap Necessary to Select Tableview Item with Search Bar
Core Data Entity Unique Constraint Does Not Work
Compiler Error When Assigning the Delegate for a Protocol in Swift iOS