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)
- 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 :
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 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).
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
Url Opening Swift App - Open Works - Called Function Does Not Work
Why HTML5 Video Doesn't Play in iOS 8 Webapp(Webview)
Translate Just 4 Lines of Code from Objective C to Swift (Pointers)
How to Implement a "Rate Us" Feature in a Phonegap App
iOS: Is Possible to Rounder Radius with Different Value in Each Corner
How to Figure Out When a HTML5 Video Player Enters The Full Screen Mode on iOS/Ipads
iOS Where to Put Custom Cell Design? Awakefromnib or Cellforrowatindexpath
How to Make One Side of a Div Pointy with CSS
Force Wkwebview to Show Mobile Version
Urlsession.Shared.Datatask VS Datataskpublisher, When to Use Which
Get The Data from All Children in Firebase Using Swift
Selecting All The Items in UIcollectionview iOS, Even The Cells That Are Not Visible
iPhone /iOS: Presenting HTML 5 Keyboard for Postal Codes
How to Go to Specific Native View Controller from React-Native Code