I am trying to change my two labels heights defined in custom cell but failed many times. Height of labels should change with length of text populated in. I have successfully implemented the cell height in heightForRowAtIndexPAthbut label height is not adjusting according to cell height. It either truncate the text or shrink the font. SetNumberOfLines to zero seems to have no effect. ContentTextView and TransView are my two uiLabels
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Customcell";
CustomeCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSString *text = [_detailItem objectAtIndex:[indexPath row]];
NSString *text2 = [_detailItem2 objectAtIndex:[indexPath row]];
CGSize maxSize = CGSizeMake(296.f, FLT_MAX);
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];
NSMutableAttributedString *attributedString2 = [[NSMutableAttributedString alloc] initWithString:text2];
CGRect rect = [attributedString boundingRectWithSize:maxSize
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
CGRect rect2 = [attributedString2 boundingRectWithSize:maxSize
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
CGSize maximumLabelSize = CGSizeMake(500,CGFLOAT_MAX);
CGSize requiredSize = [cell.contentTextView sizeThatFits:maximumLabelSize];
CGSize requiredSize2 = [cell.transView sizeThatFits:maximumLabelSize];
CGRect labelFrame = cell.contentTextView.frame;
CGRect labelFrame2 = cell.transView.frame;
labelFrame.size.height = requiredSize.height;
labelFrame2.size.height=requiredSize2.height;
cell.contentTextView.frame = labelFrame;
cell.transView.frame=labelFrame2;
[attributedString addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, [text length])];
[attributedString2 addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, [text2 length])];
[cell.contentTextView setLineBreakMode:NSLineBreakByWordWrapping];
[cell.contentTextView setMinimumScaleFactor:FONT_SIZE];
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
[style setLineSpacing:2];
[style setAlignment:(NSTextAlignment)kCTRightTextAlignment];
[cell.contentTextView setNumberOfLines:0];
[ cell.transView setNumberOfLines:0];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
tableView.separatorColor = [UIColor colorWithRed:0.576 green:0.796 blue:0.008 alpha:1];
cell.contentTextView.attributedText = attributedString;
cell.transView.attributedText=attributedString2;
return cell;
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * cellIdentifier = #"Cell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell==nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
UILabel *lblText = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, 275, [self dynamicHeightAtIndexPath:indexPath])];
lblText.backgroundColor = [UIColor clearColor];
[lblText setTag:1];
[cell.contentView addSubview:lblText];
}
UILabel *lbl = (UILabel *)[cell.contentView viewWithTag:1];
lbl.numberOfLines=0;
lbl.attributedText = #"label text";
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [self dynamicHeightAtIndexPath:indexPath]+20;
}
-(CGFloat)dynamicHeightAtIndexPath:(NSIndexPath *)indexPath
{
CGSize maximumSize = CGSizeMake(275, 9999);
UIFont *myFont =[UIFont fontWithName:#"Halvetica" size:12]; //your font for label
CGSize stringsize = [[self.array objectAtIndex:indexPath.row] sizeWithFont:myFont
constrainedToSize:maximumSize
lineBreakMode:NSLineBreakByWordWrapping];
return stringsize.height;
}
I fixed it by setting constraints on story board. In my case, i had calculate the cell height but my labels height were not adjusting accordingly. I follow the following steps.
1- On story board, click on 'Pin' appeared on bottom and set constraints (click on labels turn by turn).
2- In size inspector, change the intrinsic size of each label to "place holder".
3- Make sure to fix all warnings after you set your constraints otherwise it won't work
Related
I am trying to fill the cell.textLabel with text. That text varies in the number of lines per object in the array, and as such the cell needs to adjust in height. The cell doesn't adjust in height until the user scrolls through the list.
Here in the code.
Image of IB
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.textLabel.numberOfLines = 0;
cell.textLabel.font = [UIFont fontWithName:#"Helvetica" size:17.0];
cell.textLabel.text = [kanyeLines objectAtIndex:indexPath.row];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellText =[kanyeLines objectAtIndex:indexPath.row];
UIFont *cellFont = [UIFont fontWithName:#"Helvetica" size:17.0];
NSAttributedString *attributedText =
[[NSAttributedString alloc]
initWithString:cellText
attributes:#
{
NSFontAttributeName: cellFont
}];
CGRect rect = [attributedText boundingRectWithSize:CGSizeMake(tableView.bounds.size.width, CGFLOAT_MAX)
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
return rect.size.height + 20;
}
So I'd accidentally set the datasource twice for the table instead of setting a delegate.
I am creating a Iphone app with UITableView. In that I want to change the cell size with respect to text. I used the following code. But it fails.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// CGRect screenBounds = [[UIScreen mainScreen] bounds];
// CGSize screenSize = screenBounds.size;
NSStringDrawingContext *ctx = [NSStringDrawingContext new];
NSAttributedString *aString = [[NSAttributedString alloc] initWithString:[message objectAtIndex:0]];
UITextView *calculationView = [[UITextView alloc] init];
[calculationView setAttributedText:aString];
CGRect textRect = [calculationView.text boundingRectWithSize:self.view.frame.size options:NSStringDrawingUsesLineFragmentOrigin attributes:#{NSFontAttributeName:calculationView.font} context:ctx];
// CGSize size = [calculationView sizeThatFits:CGSizeMake(screenSize.width, FLT_MAX)];
return textRect.size.height;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
[cell.textLabel setLineBreakMode:NSLineBreakByWordWrapping];
[cell.textLabel sizeToFit];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.imageView.image = [UIImage imageNamed:#"gg.jpg"];
cell.textLabel.text = [NSString stringWithFormat:[message objectAtIndex:0], indexPath.row];
return cell;
}
Thanks in advance.
first you need to get text size.. and according this size you can set cell height.. like this example
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGSize contsize={260.00f,3000.00f};
UIFont *font=[UIFont fontWithName:#"Open Sans" size:15.0];
NSString *strt11=#"comment_text"
CGSize fontSize=[test sizeWithFont:font constrainedToSize:contsize lineBreakMode:NSLineBreakByWordWrapping];
if (fontSize.height>22)
return fontSize.height+60.0f;
return 80.0f;
}
and also put this code in cellForRowAtIndexPath method for set uilabel height..
Please use the below code.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *text = [array_loaddata objectAtIndex:indexPath.row] ;
UIFont *font = [UIFont systemFontOfSize:10];
NSAttributedString *attributedText =
[[NSAttributedString alloc]
initWithString:cellText
attributes:#
{
NSFontAttributeName: cellFont
}];
CGRect rect = [attributedText boundingRectWithSize:(CGSize){kWIDTH_FOR_MESSAGE_LABEL, MAXFLOAT}
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
return rect.size.height + 75;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier =[NSString stringWithFormat:#"Cell%d",indexPath.row] ;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
NSString *text = [array_loaddata objectAtIndex:indexPath.row] ;
UIFont *font = [UIFont systemFontOfSize:10];
NSAttributedString *attributedText =
[[NSAttributedString alloc]
initWithString:cellText
attributes:#
{
NSFontAttributeName: cellFont
}];
CGRect rect = [text boundingRectWithSize:(CGSize){kWIDTH_FOR_MESSAGE_LABEL, MAXFLOAT}
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(46, 0,kWIDTH_FOR_MESSAGE_LABEL, rect.size.height)];
label.numberOfLines = 0;
label.textColor = [UIColor grayColor];
label.lineBreakMode = NSLineBreakByWordWrapping;
label.text = (text ? text : #"");
label.font = font;
label.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:label];
[label release];
}
As Simple as that :
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewAutomaticDimension;
}
I have successfully resized my UITableViewCell to fit it's content, but my UILabel is not resizing properly. This is how I'm resizing the cell:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *str = #"My really long text";
CGSize constrainedSize = CGSizeMake(250, 9999);
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:#"HelveticaNeue-Light" size:16.0], NSFontAttributeName,
nil];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:str attributes:attributesDictionary];
CGRect requiredHeight = [string boundingRectWithSize:constrainedSize options:NSStringDrawingUsesLineFragmentOrigin context:nil];
if (requiredHeight.size.width > 250) {
requiredHeight = CGRectMake(0,0, 250, requiredHeight.size.height);
}
if (requiredHeight.size.height + 10 >= 60)
return requiredHeight.size.height + 10;
else
return 60;
}
I create my UILabel in my prototype cell in Storyboard
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Post-Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
UILabel *text = (UILabel *)[cell viewWithTag:2];
text.text = #"My Label's Text";
[text sizeToFit];
return cell;
}
I use this for my dynamic labels. First I know my label is constrained over 60px from the left and 67 from the right. So I know my label will have the screen width minus that padding to fit its content before wrapping. This method will give me the height of the title no matter how many lines. I set a lowest height to 44 so that even if the user has uber small text I still have a nice sized cell. The label in my cell is 11 px from the top and 11 from the bottom of the content view so I add 22 to the height for padding.
+ (CGFloat)cellHeightForTitle:(NSString*)title
{
UIFont *font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
NSString *text = title ?: #"test";
CGFloat hotizontalPadding = 127;
CGFloat desiredWidth = [UIScreen mainScreen].bounds.size.width - hotizontalPadding;
NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text attributes:#{NSFontAttributeName: font}];
UILabel *label = [[UILabel alloc] init];
label.attributedText = attributedText;
label.numberOfLines = 0;
label.lineBreakMode = NSLineBreakByWordWrapping;
CGSize size = [label sizeThatFits:CGSizeMake(desiredWidth, CGFLOAT_MAX)];
font = nil;
attributedText = nil;
return MAX(44, size.height + 22);//top + bottom padding
}
Then in my table I call
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [MyCell cellHeightForTitle:#"Some long title"];
}
a boy of StackOverflow had given such help to set the height of the cells automatically based on the content of the label ...
I have implemented his method but when I scroll on tavleview, all the text in each cell overlaps and do not read anything ... Can you tell me what 's wrong with this code?
I had to change CGSize because I'm working on some code ios7 was deprecated for example:
CGSize size = [comment sizeWithFont: [UIFont systemFontOfSize: 14] constrainedToSize: lineBreakMode constraint: UILineBreakModeWordWrap];
Can you help me please?
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
PFObject *object = [self objectAtIndexPath:indexPath];
//Here you are getting the comment again. This is necessary to determine how tall you need
//the cell to be
NSString *comment = [object objectForKey:(#"Testo")];
// Again you are getting the constraints because you are going to us this size
// to constrain the height of the cell just like you determined the size for the label.
CGSize constraint = CGSizeMake(250 - (10 * 2), 10000.0f);
// Again, determining the size that we will need for the label, because it will drive
// the height of the cell
UIFont *FONT = [UIFont systemFontOfSize:11];
CGSize size = [comment boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin attributes:#{NSFontAttributeName:FONT }context:nil].size;
// Finally, we can determine the height for the cell!
CGFloat height = MAX(size.height, 44.0f);
// return the height, which is the height of the label plus some extra space to make
// it look good.
return height + (10 * 2);
}
// Override to customize the look of a cell representing an object. The default is to display
// a UITableViewCellStyleDefault style cell with the label being the first key in the object.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSString *text = [object objectForKey:#"Testo"];
CGSize constraint = CGSizeMake(250 - (10 * 2), 10000.0f);
// This is determining the size that you will need for the label based on the comment
// length and the contraint size
UIFont *FONT = [UIFont systemFontOfSize:11];
CGSize size = [text boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin attributes:#{NSFontAttributeName:FONT }context:nil].size;
// Here you are creating your label and initializing it with the frame that you have
// determined by your size calculations above
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, MAX(size.height, 44.0f) + 20.0f)];
// setting up the label properties
label.numberOfLines = 0;
label.lineBreakMode = NSLineBreakByWordWrapping;
label.text = text;
label.font = [UIFont systemFontOfSize:11];
// adding the label view to the cell that you have created
[cell.contentView addSubview:label];
// Configure the cell
return cell;
}
Solved
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSString *comment = [[self.objects objectAtIndex:[indexPath row]] objectForKey:#"Testo"];
CGFloat whidt = 260;
UIFont *FONT = [UIFont systemFontOfSize:15];
NSAttributedString *attributedText =[[NSAttributedString alloc] initWithString:comment attributes:# { NSFontAttributeName: FONT }];
CGRect rect = [attributedText boundingRectWithSize:(CGSize){whidt, MAXFLOAT}
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
CGSize size = rect.size;
return size.height +130;
}
- (FFCustomCellTimeline *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
FFCustomCellTimeline *cell = (FFCustomCellTimeline * )[self.tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (!cell) {
cell = [[FFCustomCellTimeline alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
}
NSString *text = [object objectForKey:#"Testo"];
CGSize constraint = CGSizeMake(260 , 20000.0f);
UIFont *FONT = [UIFont systemFontOfSize:15];
CGSize size = [text boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin attributes:#{NSFontAttributeName:FONT }context:nil].size;
return cell;
}
I am creating UITableView, where each cell size will b calculated based on the text I input.
I am Calculating size of Cell in heighForRowAtIndexPath
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSString *text = [tableData objectAtIndex:[indexPath row]];
CGSize constraint = CGSizeMake(210.0f, 20000.0f);
size = [text sizeWithFont:[UIFont systemFontOfSize:12.0f] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
height = MAX(size.height, 44.0f);
return height + (CCM * 2);
}
this is how I am calculating height for each cell, now my question is - I want to add image to each cell like comment box? like this
Click here to see comment boxes I am looking for
You can see these kind of comment boxes
what I did is I made my image into two parts
first one has top part which is working well on every cell starting image is coming
now while fixing bottom part me facing problem
for small text in cell UILable its working well
when size of text in UILabel increased it's not getting properly adjusted
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier = #"cell";
NSLog(#"index path %d", indexPath.row);
MyTableCell *cell = (MyTableCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
cell = [[[MyTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
[cell.cellBackgroundBottom setFrame:CGRectMake(75.0f, 92.0f, 228.0f, height + 25.0f)];
[cell.cellContent setText:(NSString*)[tableData objectAtIndex:[indexPath row]]];
[cell.cellContent sizeToFit];
CGRect theFrame = [cell.cellContent frame];
theFrame.size.width = 210.0f;
[cell.cellContent setFrame: theFrame];
return cell;
}
I have created one UITableView subclass naming MYTableCell where I have defined what all I need inside my each cell.
// Creating UILabel for cell content or comments block
cellContent = [[UILabel alloc]initWithFrame: CGRectMake( 90.0f, 15.0f, 210.0f, 60.0f)] ;
[cellContent setLineBreakMode:UILineBreakModeWordWrap];
cellContent.numberOfLines = 0;
[cellContent setMinimumFontSize:15.0f];
[cellContent setBackgroundColor:[UIColor clearColor]];
// [cellContent setTextColor:[UIColor colorWithRed:73.0f green:73.0f blue:73.0f alpha:1.0f]];
[cellContent setFont:[UIFont fontWithName:#"Helvetica" size:12.0f]];
cellContent.textAlignment = UITextAlignmentLeft;
[cellContent setTag:1];
[self addSubview:cellContent];
[cellContent release];
cellBackgroundTop = [[UIImageView alloc] initWithFrame:CGRectMake(60.0f, 5.0f, 243.0f, 13.5f)];
[cellBackgroundTop setImage:[UIImage imageNamed:#"comments.png"]];
[self addSubview:cellBackgroundTop];
[cellBackgroundTop release];
cellBackgroundBottom = [[UIImageView alloc] initWithFrame:CGRectZero];
[cellBackgroundBottom setImage:[UIImage imageNamed:#"Untitled-1_09.png"]];
[self addSubview:cellBackgroundBottom];
[cellBackgroundBottom release];
I got answer for my question
. This is what i have to do in my cellForRowAtIndexPath
NSString *text = [tableData objectAtIndex:[indexPath row]];
CGSize constraint = CGSizeMake(210.0f, 20000.0f);
CGSize size1 = [text sizeWithFont:[UIFont systemFontOfSize:12.0f] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGFloat height1 = MAX(size1.height, 44.0f);
[cell.cellBackgroundBottom setFrame:CGRectMake(75.0f, 18.0f, 228.0f, height1 + 10.0f)];