How to Set Uitextview's Height Dynamically in Uitableviewcell Based on String Size

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

Sample Image

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 the UITableViewCell
  • 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 the leading, trailing, top, and bottom edges of your UITextView to the corresponding edges of the contentView.
  • Set the row height to UITableViewAutomaticDimension instead of a hardcoded CGFloat.
  • 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



Leave a reply



Submit