Change Cell Height by The Content of The Textview Inside The Cell

Change cell height by the content of the textView inside the cell

Bound Your textview with cell from all sides using marginal constraints.(Leading, Trailing, Top and Bottom constraints)

Sample Image

  • Disable textView Scrolling

In viewDidLoad() add the following.

tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension

This will make your cell size according to your textview content size.

Have a look at result :

Sample Image

You don't need to write heightForRowAtIndexPath.

Dynamically change cell's height while typing text, and reload the containing tableview for resize

Using tableView's [tableView beginUpdates] and [tableView endUpdates] will solve this issue. See this answer for more information.

Dynamic cell height for textView Swift

set the height of textView and connect it to view controller as NSLayoutConstraints and update it via the textViewDidChange.

See the below code in which textViewHeight is the NSLayoutConstraints

func textViewDidChange(_ textView: UITextView) {

let fixedWidth = textView.frame.size.width
textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude))
let newSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude))
var newFrame = textView.frame
newFrame.size = CGSize(width: max(newSize.width, fixedWidth), height: newSize.height)
textView.frame = newFrame;
textViewHeight.constant = newFrame.size.height
tableView.beginUpdates()
tableView.endUpdates()
}

Adjust height of UITableViewCell to height of UITextView

I think you need to use self-sizing UITableViewCell. Replace your current implementation of heightForRowAt with the following:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

return UITableViewAutomaticDimension
}

Now height of cells in your UITableView object will be calculated automatically based on constraints.
Also, add some estimated value for row height:

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {

return 100.0 // You can set any other value, it's up to you
}

Now you will see that the UITextView view fills the whole UITableViewCell cell.

Automatic height for TextView using autolayout in cell for tableView

Solved this way.
Change the TextView to UILabel, Then set Lines property in the attribute inspector to 0.

on viewDidLoad

    tableview.rowHeight = UITableView.automaticDimension
tableview.estimatedRowHeight = 229.0

It works.

More https://www.raywenderlich.com/8549-self-sizing-table-view-cells

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).

how to dynamically change the textview height and cell height according to the text length without the scroll

You can achieve this by adaptive layout. Check this awesome tutorial if you are working with autolayout. Else you can set dynamic tableView cell height by calculating the height in which your text will fit.

You can calculate height of text by using below method. Pass text, required font and width of your textview.

-(CGFloat)heightForText:(NSString*)text withFont:(UIFont *)font andWidth:(CGFloat)width
{
CGSize constrainedSize = CGSizeMake(width, MAXFLOAT);
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName,nil];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:text attributes:attributesDictionary];
CGRect requiredHeight = [string boundingRectWithSize:constrainedSize options:NSStringDrawingUsesLineFragmentOrigin context:nil];
if (requiredHeight.size.width > width) {
requiredHeight = CGRectMake(0,0,width, requiredHeight.size.height);
}
return requiredHeight.size.height;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return [self heightForText:@"your text view text for this row" withFont:[UIFont fontWithName:@"Helvetica" size:16] andWidth:320];
}


Related Topics



Leave a reply



Submit