I am working on an app which requires me to show pictures and some text in TableView. These pictures can be of different heights and so I need to vary the cell height accordingly. so I have overridden this method :
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath
If I have a single static value for cell identifier then the height of the image inside the cell cannot vary dynamically.
So do I need to have different values of Cell Identifier for each cell ? Is there some other way ?
I cannot use some other view than Tableview because I need to show some cells dynamically in between based on user interaction.
Thanks.
You should reference your data model to get the image size and return the row height.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
UIImage *image = dataModel[indexPath.row];
return image.size.height + 16.0; //16 is for padding
}
If you subclass your cell, you can adjust the imageView frame in -layoutsubviews (after super):
- (void)setupImageView
{
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.clipsToBounds = YES;
self.contentImageView = imageView;
[self.contentView addSubview:imageView];
}
- (void)layoutSubviews
{
[super layoutSubviews];
CGRect bounds = self.contentView.bounds;
float margin = 8.0;
CGRect textViewFrame = CGRectZero;
textViewFrame = CGRectMake(margin, roundf(margin * 2.0), bounds.size.width - (margin * 2.0), roundf(bounds.size.height - (margin * 4.0)));
self.contentImageView.frame = textViewFrame;
}
For dynamic tableViewCell height use this.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGSize labelHeight = [self heigtForCellwithString:yourLabel.text withFont:yourLabel.font];
return labelHeight.height; // the return height + your other view height
}
-(CGSize)heigtForCellwithString:(NSString *)stringValue withFont:(UIFont)font{
CGSize constraint = CGSizeMake(300,9999); // Replace 300 with your label width
NSDictionary *attributes = #{NSFontAttributeName: font};
CGRect rect = [stringValue boundingRectWithSize:constraint
options: (NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
attributes:attributes
context:nil];
return rect.size;
}
Example for only an image:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//get reference to a image
UIImage *image = [arrImages objectAtIndex:indexPath.row];
//get the height of your imageview on the cell
CGFloat imageViewHeight = image.size.height/2;//RetinaDisplay
//return the height of the imageview (plus some padding) for your cell height
return imageViewHeight; //add here also all other constant height's of objects that you have on your cell.
}
For a more comprehensive example look at my answer here:
Resize Custom cell with a UILabel on it based on content text from JSON file
Calculate the Cell Height
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSString *text = [items objectAtIndex:[indexPath row]];
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = MAX(size.height, 44.0f);
return height + (CELL_CONTENT_MARGIN * 2);
}
For more reference see the Link..
You should calculate the height of each cell before the cell display.So,You could write a method to get the height,and then use the method you written in the Model.
Related
I want to change the height of my tableview cell according to the amount of text by using auto layout. I have tried the following code but it doesn't work:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
AddTaskDetails *addTaskDetail = (self.tasksArray)[indexPath.row];
CGFloat height;
float textcount = [addTaskDetail.taskDetail length];
if(textcount>60)
{
height = textcount-20;
NSLog(#"%d,%f",indexPath.row,height);
}
else
{
height = 70;
}
return height;
}
You better don't hardcode the height required for the string. Rather use the attributed text height property.
let attributes = [NSFontAttributeName : textFont,
NSForegroundColorAttributeName : UIColor(
red:25/255,
green:176/255,
blue:37/255,
alpha:1.0)]
let attrString:NSAttributedString? = NSAttributedString(string: yourString, attributes: attributes)
let rect:CGRect = attrString!.boundingRectWithSize(CGSizeMake(280.0,CGFloat.max), options: NSStringDrawingOptions.UsesLineFragmentOrigin, context:nil )
return rect.height
You will need to use boundingRectWithSize:options:attributes:context on the string to be rendered.
This is a frequently asked question and you may find code snippets when you search for 'UITableViewCell with dynamic height'.
Try with this...
it will help you.
NSString *classSubjecttxt =#"Some text";
CGSize requiredSizeSubjetc =[classSubjecttxt sizeWithFont:[UIFont fontWithName:#"Trebuchet MS" size:12] constrainedToSize:CGSizeMake(labelwidth, CGFLOAT_MAX)];
int height=YOUR DEFAULT HEIGHT;
if(requiredSizeSubjetc.height >18){
height=height-18+ceil(requiredSizeSubjetc.height);
}
return height;
You don't have to do it programmatically. You can easily do it using Autolayouts in interface builder.
Just add a UITableViewCell to you UITableView
Set its style to custom
Make sure its size in Size inspector is default
Add a UILabel to this cell
Set its Top, Bottom, Left, Right Constraints
In size inspector set preferred width to explict
In attribute inspector set number of lines to "0"
Then add these lines in viewDidLoad()
tableView.estimatedRowHeight = 40.0
tableView.rowHeight = UITableViewAutomaticDimension
Also implement these delegate methods
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 44
}
- (CGFloat)getLabelHeight:(NSString*)textvalue
{
if (![textvalue isEqualToString:#""]) {
NSString *string=textvalue;
string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
UIFont *font = [UIFont systemFontOfSize:12];
CGSize constraint = CGSizeMake(SCREENWIDTH/1.0,NSIntegerMax);
NSDictionary *attributes = #{NSFontAttributeName: font};
CGRect rect = [string boundingRectWithSize:constraint
options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
attributes:attributes
context:nil];
return rect.size.height;
}
else
{
return 20;
}
}
Then You can get size by calling this :
AddTaskDetails *addTaskDetail = (self.tasksArray)[indexPath.row];
CGFloat textHeight = [self getLabelHeight:[addTaskDetail valueForKey:#"YOURKEY"]];
Here You will get the text size of your text then return it to heightForRowAtIndexPath.
First of all you need to calculate height of your label.
You can get dynamic height of your label by calling with below functions:
-(CGFloat)getDynamicHeightOfLabelWithFont:(UIFont *)font withText:(NSString *)text withFrame:(CGRect)initialFrame
{
UILabel *lblDummy = [[UILabel alloc] initWithFrame:initialFrame];
lblDummy.font = font;
lblDummy.lineBreakMode = NSLineBreakByWordWrapping;
lblDummy.numberOfLines = 0;
lblDummy.text = text;
CGRect dummyFrame = initialFrame;
dummyFrame.size = [lblDummy sizeThatFits:initialFrame.size];
return dummyFrame.size.height;
}
You need to call this function on heightForRowAtIndexPath and return the height.
and you need to set the frame on cellForRowAtIndexPath and set frame to your label.
1.Create a custom cell class. Create outlets for label/imageview.
2.Add this method in your custom cell class.
-(void) layoutSubviews
{
[super layoutSubviews];
[self.contentView layoutIfNeeded];
self.yourLabel.preferredMaxLayoutWidth = CGRectGetWidth(self.sentenceLabel.frame);
}
3.In your view controller class,create a property of your custom cell class.
-(DynamicTblVCell *)prototypeCell
{
if(!_prototypeCell)
{
_prototypeCell = [self.tableView dequeueReusableCellWithIdentifier:#"DynamicTblVCell"];
}
return _prototypeCell;
}
4. In your viewDidLoad add these two lines:
self.tableView.estimatedRowHeight = 100.0;
self.tableView.rowHeight = UITableViewAutomaticDimension;
5. And finally do this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"DynamicTblVCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
//to configure a cell before it is displayed
[self configureCell:cell forRowAtIndexPath:indexPath];
return cell;
}
-(void)configureCell:(UITableViewCell *)cell forRowAtIndexPath: (NSIndexPath *)indexPath
{
if([cell isKindOfClass:[DynamicTblVCell class]])
{
DynamicTblVCell * textCell = (DynamicTblVCell *)cell;
textCell.sentenceLabel.text = [NSString stringWithFormat:#"jhjshdjshdjhkjdhajsdhajsdh"];
textCell.sentenceLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
}
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
[self configureCell:self.prototypeCell forRowAtIndexPath:indexPath];
self.prototypeCell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(self.tableView.bounds), CGRectGetHeight(self.prototypeCell.bounds));
[self.prototypeCell layoutIfNeeded];
CGSize size = [self.prototypeCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
return size.height+1;
}
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewAutomaticDimension;
}
set number of lines for label to 0.
Add constraints for top space, bottom space, left and right space. Do not add height constraint for label.
Use below code to get better result with/without Autolayout. Need to calculate font height of label and set the position as per your requirement.
It will also helpful for calculate collectionView's dynamic cell height.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGSize constraint = CGSizeMake(screenWidth - 62, 20000.0f);
CGSize size;
NSStringDrawingContext *context = [[NSStringDrawingContext alloc] init];
CGSize boundingBox = [string boundingRectWithSize:constraint
options:NSStringDrawingUsesLineFragmentOrigin
attributes:#{NSFontAttributeName:self.titleLabel.font}
context:context].size;
size = CGSizeMake(ceil(boundingBox.width), ceil(boundingBox.height));
return size.height + 16;
}
I am creating an application with iOS 7 devices. This application contains UITableView with dynamic cell height.
To create this I have a custom cell which contains methods:
- (CGFloat)heightWithModel:(Model *)model
{
CGFloat height = 0.0f;
height = self.contentLabel.frame.origin.y + [self contentHeightWithModel:model] + CellBottomOffset;
return height;
}
and...
- (CGFloat)contentHeightWithModel:(Model *)model
{
CGFloat height = 0.0;
NSDictionary *attributes = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14.0f] forKey:NSFontAttributeName];
NSString *string = model.content;
NSStringDrawingContext *context = nil;
NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin;
CGSize size = CGSizeMake(self.contentLabel.bounds.size.width, CGFLOAT_MAX);
CGRect frame = [string boundingRectWithSize:size options:options attributes:attributes context:context];
height = frame.size.height;
return height;
}
In my view controller, I have implemented UITableViewDelegate protocol method:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat height;
static Cell *cell;
if (!cell)
{
cell = [tableView dequeueReusableCellWithIdentifier:CellReuseIdentifier];
}
height = [cell heightWithModel:[self.dataSource.models objectAtIndex:indexPath.row]];
return height;
}
As far as I understand, this should be enough to create a table view with dynamic cell height. Despite this, I have a table view like this:
As you see, part of text are hidden, because label (red one) height is too small. Cell height is set dynamically by using Auto Layout (10 px from the bottom).
Can anyone see where is the problem?
First take a variable
CGFloat height;
put this lines in viewdidload method
in that set estimated height of cell and for that label set lines to 0 in inspector panel and change its height = to >= and give constraint from all side
[self.tbl_rating layoutIfNeeded];
[self.tbl_rating setNeedsLayout];
if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1 )
{
self.tbl_rating.estimatedRowHeight=153.0f;
self.tbl_rating.rowHeight=UITableViewAutomaticDimension;
}
Now put this two method for finding label height and dynamic cell height increment
In this method set your label text that you retriving from like array or web service
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// 7.1>
if (NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_7_1 ) {
height=[self findHeightForText:[NSString stringWithFormat:#"%#",[[arr_review valueForKey:#"desc"]objectAtIndex:indexPath.row]] havingWidth:self.view.frame.size.width andFont:[UIFont systemFontOfSize:12.0f]].height;
return 153+height;
}
return UITableViewAutomaticDimension;
}
-(CGSize)findHeightForText:(NSString *)text havingWidth:(CGFloat)widthValue andFont:(UIFont *)font {
CGSize size = CGSizeZero;
if (text) {
CGRect frame = [text boundingRectWithSize:CGSizeMake(widthValue, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:#{ NSFontAttributeName:font } context:nil];
size = CGSizeMake(frame.size.width, ceil(frame.size.height));
}
return size;
}
NOTE: In tableview method heightForRowAtIndexPath I recommand you pass text from array value. If this thing doesnt work then check your constraints.
This thing works for me Hope it will work.
Thank you.
I have a long label that is my first label, and I want to fit it in my cell. This is what I have but it isn't working.
I have a custom UITabelviewCell with a few labels in it.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
switch (indexPath.row) {
case 0:{
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [diningHallTimes[indexPath.row][#"description"] sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
CGFloat height = MAX(size.height, 44.0f);
return height + (CELL_CONTENT_MARGIN * 2) + 40;
// return myStringSize.height;
break;
}
default:
return 40;
break;
}
}
Here is cell for row
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
DiningInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [diningHallTimes[indexPath.row][#"description"] sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
CGFloat height = MAX(size.height, 44.0f);
CGFloat the = height + (CELL_CONTENT_MARGIN * 2);
cell.descriptionLabel.numberOfLines = 0;
[cell.descriptionLabel setFrame:CGRectMake(20, 0, 280, the)];
NSLog(#"%f", the);
cell.descriptionLabel.text = diningHallTimes[indexPath.row][#"description"];
cell.daysLabel.text = diningHallTimes[indexPath.row][#"days"];
cell.timeLabel.text = diningHallTimes[indexPath.row][#"time"];
return cell;
}
But then this is what my cell looks like
Not sure why this is happening, I am running iOS8, but I need it to work for both is and 7.
Thanks for the help in advance.
You need to calculate boundingRectWithSize: for label text. Also need to calculate number of lines required for updated content also. Try this below method to calculate new frame for label.
- (UILabel *) updateLabelFrame:(UILabel *)label {
CGRect lblRect = label.frame;
CGSize maxSize = CGSizeMake(label.frame.size.width, MAXFLOAT);
CGRect labelRect = [label.text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:#{NSFontAttributeName:label.font} context:nil];
CGFloat labelHeight = labelRect.size.height;
// For below line 16 is default height (The height before content is set). Change this value
// as per your requirement
int lines = labelHeight / 16; // Here 16 is a fix height (default height) for label.
[label setNumberOfLines:lines];
lblRect.size.height = labelHeight;
[label setFrame:lblRect];
return label;
}
Edit:
You can place this method any where you want. Like some base class or in same view controller. Here I modified above method which will return label with dynamic frame.
For your case you need to place this method in view controller class. Then call this method in -cellForRowAtIndexPath after content is updated for label. See below code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
DiningInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [diningHallTimes[indexPath.row][#"description"] sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
CGFloat height = MAX(size.height, 44.0f);
CGFloat the = height + (CELL_CONTENT_MARGIN * 2);
cell.descriptionLabel.numberOfLines = 0;
[cell.descriptionLabel setFrame:CGRectMake(20, 0, 280, the)];
NSLog(#"%f", the);
cell.descriptionLabel.text = diningHallTimes[indexPath.row][#"description"];
cell.daysLabel.text = diningHallTimes[indexPath.row][#"days"];
cell.timeLabel.text = diningHallTimes[indexPath.row][#"time"];
// Update descriptionLabel height.
cell.descriptionLabel = [self updateLabelFrame:cell.descriptionLabel];
return cell;
}
You have to set up Autolayout constraints for the label and create a prototype cell with the content. And then you have to return then you have to get the height of the cell.
For reference , you could see the following video
https://www.youtube.com/watch?v=6KImie4ZMwk
Try [yourLabel sizeToFit] or you may use [yourLabel sizeThatFit:CGFrame];
I have a UILabel in a cell and I need to change the height of the UILabel dynamically. So the cell is a xib file with height 44 and label 240x44 and label autosizing horizontally and vertically. The font is 15.0f size, system. Number of lines = 0, wrap by word.
So I have the method
+ (CGFloat) cellHeightForString: (NSString *) string
{
CGFloat cellHeight = [string sizeWithFont:[UIFont systemFontOfSize:15.0f] constrainedToSize:CGSizeMake(240.0f, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping].height;
return cellHeight < 44.0f ? 44.0f : cellHeight;
}
but the height of the cell is sometimes smaller so not all lines of the text are shown in the cell. Can't understand what I'm doing wrong.. Any help?
Try this,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// add your code snippet here
// dynamically change the label height,
CGSize textSize = {
200.0, // limit width
20000.0 // and height of text area
};
CGSize contentSize = [yourText sizeWithFont:[UIFont systemFontOfSize:15.0] constrainedToSize:textSize lineBreakMode:NSLineBreakByWordWrapping];
CGFloat contentHeight = contentSize.height < 36? 36: contentSize.height; // lower bound for height
CGRect labelFrame = [yourLabel frame];
yourLabel.size.height = contentHeight;
[yourLabel setFrame: labelFrame];
[yourLabel setText:yourText];
return cell;
}
Dynamically change the cell height,
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
YourCell *currentCell = (YourCell*)[self tableView:tableView cellForRowAtIndexPath:indexPath];
// change hard coded value based on your cell alignment
int cellLength=[currentCell.yourLabel.text length]/42;
cellLength = cellLength*22 > 200 ? cellLength*22 : 200;
CGSize constraint = CGSizeMake((200 - 10), cellLength);
CGSize size1 = [cellText sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
return 220+size1.height;
}
This question already has answers here:
Change the label width dynamically inside a UITableViewCell
(3 answers)
Closed 9 years ago.
I am trying to set the label width dynamically based on the content of the other label on the same line.
I am implementing the logic inside the "cellForRowAtIndexPath"
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"rowNumber:%d", indexPath.row);
EntityTableViewCell *cell = nil;
static NSString *CellIdentifier = #"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.companyNameLabel.text = [group1 objectAtIndex:indexPath.row];
cell.companyCROfficeAddrLabel.text = [group2 objectAtIndex:indexPath.row];
cell.companyBusinessAddrLabel.text = [group3 objectAtIndex:indexPath.row];
cell.timestamp.text = [Utils getDateTimeString:[group4 objectAtIndex:indexPath.row]];
[cell.companyLogoImageView setImageWithURL:[NSURL URLWithString:[myArray objectAtIndex:indexPath.row] ]
placeholderImage:[UIImage imageNamed: #"profile-image-placeholder"]
options:indexPath.row == 0 ? SDWebImageRefreshCached : 0];
//logics to dynamically change the label width to maximum utilize the realstate
CGFloat timeStampWidth = [cell.timestamp.text sizeWithFont:cell.timestamp.font].width;
CGFloat ksCompanyNameLableMaxWidth = 235;
NSLog(#"timeStampWidth:%f", timeStampWidth);
CGSize companyNameLableSize = CGSizeMake((ksCompanyNameLableMaxWidth - timeStampWidth), cell.companyNameLabel.frame.size.height);
CGRect newFrame = cell.companyNameLabel.frame;
newFrame.size = companyNameLableSize;
cell.companyNameLabel.frame = newFrame;
NSLog(#"companyLableWidth:%f", newFrame.size.width);
return cell;
}
There are multiple problems with this code.
As the table is initialised, although this piece of code is called for every cell. The width of the label is still set as the size in the story board. On the other side, if I scroll down, the label is displayed correctly as designed in the code.
Because I also have a tabView controller within my app, whenever I switch between the tabs, the dynamically populated label width is getting updated with the default label width set in the storyboard again.
Could someone please tell me what I did wrong and suggest some solution?
Thanks
try this code
#define FONT_SIZE 14.0f
#define CELL_CONTENT_MARGIN 8.0f
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSString *text = [BugComments_text objectAtIndex:indexPath.row];
CGSize constraint = CGSizeMake(tableView.frame.size.width - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
CGFloat height = MAX(size.height+40, 60.0f);
return height + (CELL_CONTENT_MARGIN * 2);
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *text = [arrayofdata objectAtIndex:indexPath.row];
CGSize constraint = CGSizeMake(tableView.frame.size.width - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
[cell.Content_Text setText:text];
cell.Content_Text setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN+cell.Content_Email.frame.origin.y+cell.Content_Email.frame.size.height, tableView.frame.size.width - (CELL_CONTENT_MARGIN * 2), MAX(size.height,20.0f))];
}
Try this in your cellForRowAtIndexPath for dynamically setting the company label text,
CGSize textSize = {
200.0, // limit width
20000.0 // and height of text area
};
CGSize contentSize = [yourText sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:textSize lineBreakMode:NSLineBreakByWordWrapping];
CGFloat contentHeight = contentSize.height < 36? 36: contentSize.height; // lower bound for height
CGRect companyLabelFrame = [cell.companyNameLabel frame];
companyLabelFrame.size.height = contentHeight;
[cell.companyNameLabel setFrame:companyLabelFrame];
cell.companyNameLabel setText:yourText];
I finally solve the problem by clearly specifying it again and get the answer from #rdelmar
please look the link Here