UITableCell Height not adjusting properly - ios

I have a UITableView that lists out the details of a certain item. Some of the details require more than one line to display properly.
I am using this code to dynamically resize the cell:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 1) return 320;
CGSize maxSize = CGSizeMake(280, MAXFLOAT);
CGSize cellSize = [[[appDelegate.callList objectAtIndex:callID] objectAtIndex:indexPath.row+1]
sizeWithFont:[UIFont systemFontOfSize:17]
constrainedToSize:maxSize
lineBreakMode:NSLineBreakByWordWrapping];
return cellSize.height+10;
}
It is a cell with Right Detail style. Nothing else. The detail textlabel is size 17 font.
Using this code, I get cells that cut off the cell below:
I know I could obviously just increase the +10 at the end, but then my cells would be too big for my liking.
Any help appreciated!
EDIT
I have found the issue. The width set in maxSize was too large.
Is there any way to find out the current detaillabel with without entering it manually prior?

Please refer below link, its an example of Dynamic Height UITableView Cells in Xcode: It may help you.
http://samrayner.com/posts/dynamic-tableview-cells/

You can use
CGSize maximumSize = CGSizeMake(300, 9999);
NSString *myString = #"This is a long string which wraps";
UIFont *myFont = [UIFont fontWithName:#"Helvetica" size:14];
CGSize myStringSize = [myString sizeWithFont:myFont
constrainedToSize:maximumSize
lineBreakMode:self.myLabel.lineBreakMode];
to calculate the displaying size of a givin NSString. So you can alter your cell's height according to the codes above. And also, you should reload your tableview after the heigth of some cell is changed.

Related

UILabel can't get multiples lines on increasing dynamic height of text in iOS 7

I am developing an app, in which I am getting dynamic text and I have to increase height of label dynamically depending upon text and also increase and decrease height of table cell on basis on label height. So I wrote code both for iOS 6 and iOS7 and it works fine in iOS 6 but in iOS7 it not working fine. Below is code. Function that return height of text.
- (CGSize)getSizeOfText:(NSString *)text withFont:(UIFont *)font widthOftext:(int )txtWidth
{
CGSize boundingSize = CGSizeMake(txtWidth, 1000);
CGSize size;
if (MyDelegate.isIos7)
{
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
font, NSFontAttributeName,
nil];
CGRect frame = [text boundingRectWithSize:boundingSize
options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
attributes:attributesDictionary
context:nil];
return frame.size;
}
else
{
CGSize requiredSize = [text sizeWithFont:font constrainedToSize:boundingSize lineBreakMode:UILineBreakModeWordWrap];
size=requiredSize;
}
return size;
}
There is bit change on return of height for iOS 6 and iOS 7.
Below First four label's text height difference for both them.
iOS6
42.000000
42.000000
21.000000
42.000000
iOS7
39.674072
39.674072
20.123291
39.674072
You can see bit difference of first four texts. But One major problem more in iOS is that It always show me text in just one line and truncate text even text height goes to 39 which may come in 2 lines, I also set number of lines to 20 and also tried to set number line to just 0 but didn't work.
Kindly guide me on this if anyone already this sort of problem. Thanks
Edited
I already posted this question earlier if anyone want to see screen shots of both iOS results so check there also.
Same Thread with Screen Shots
Try to set this property to your label
label.numberOfLines = 3;
it should solve the problem and let me know.
Set the height of the label to frame.size.height+1 or round it up.
and of course make sure the numberOfLines of your label is 3 (or 0 if you think it is possible to show more than 3).
Try in this way for dynamic change of height for cell using UITableView. This works for me for IOS7 and IOS6 also
At -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellText=yourtextstring;
UIFont *cellFont = [UIFont fontWithName:#"SourceSansPro-Bold" size:13.0];
CGSize constraintSize = CGSizeMake(300.0f, MAXFLOAT);//225
CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
//NSLog(#"*** labelSize.height *** %f",labelSize.height);
return labelSize.height+250; //change this as per your need
}
At - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellText=yourtextstring;
UIFont *cellFont=[UIFont fontWithName:#"SourceSansPro-Regular" size:13.0];
CGSize constraintSize=CGSizeMake(300,MAXFLOAT);
CGSize txtViewSize=[cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
CGRect frame=cell.yourlabel.frame;
// frame.size.width=300;
frame.size.height=txtViewSize.height+150;
cell.yourlabel.frame=frame;
cell.yourlabe.text=cellText;
return cell;
}
Hope it helps you..
NO need to compare for iOS 6 and iOS 7
use like below
CGFloat txtWidth = 250.0f;
NSString *font = #"Noteworthy-Bold"; //say u hav font
NSString *text = #"Some long text Some long text Some long text Some long text Some long text Some long text Some long text Some long text Some long text Some long text Some long text Some long text Some long text"; //say u hav some text
UIFont *labelFont = [UIFont fontWithName:font size:15];
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObject:labelFont forKey:NSFontAttributeName];
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:text attributes:attributesDictionary];
CGRect frame = [attributedText boundingRectWithSize:(CGSize){txtWidth, MAXFLOAT}
options: (NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
context:nil];//you need to specify the some width, height will be calculated
CGSize requiredSize = frame.size;
NSLog(#"wdth->%f height->%f",requiredSize.width,requiredSize.width);
//248.962509 height->248.962509 //iOS 7
//248.962494 height->248.962494 //iOS 6
//almost same
//also set 'numberOfLines` property of label

iOS: How can I achieve design with dynamic length text?

How can I make a simple design like in the image below where text length is dynamic? In the image below there are two sections Ingredients and Instructions with dynamic text length. There could be more sections.
Should I go for UIView with a UIScrollView or Table View? Any kind of help would be appreciated.
Try this approach.
1. Use UITextView this will give you possibility to scroll text inside UITextView if text will be very big.
2. Get the size of you text view
// return the size for UITextView for both IOS7 and IOS6
-(CGSize) getSizeWithMaxLabelSize:(CGSize) maxLabelSize forTextViewWithText:(NSString *) text
{
CGSize expectedLabelSize;
CGSize maximumLabelSize = maxLabelSize;
if (SYSTEM_VERSION_GREATER_THAN(#"6.2")) {
NSDictionary *stringAttributes = [NSDictionary dictionaryWithObject:[UIFont fontWithName:#"Arial" size:12] forKey: NSFontAttributeName];
CGRect rect =[text boundingRectWithSize:maximumLabelSize options:NSStringDrawingUsesLineFragmentOrigin attributes:stringAttributes context:nil];
expectedLabelSize = rect.size;
expectedLabelSize.height = ceilf(expectedLabelSize.height);
expectedLabelSize.width = ceilf(expectedLabelSize.width);
} else {
expectedLabelSize = [text sizeWithFont:[UIFont fontWithName:#"Arial" size:EPCommentViewFontSize] constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByWordWrapping];
}
return expectedLabelSize;
}
3. Use this method (2) when you calculate cell height for you table
- tableView:cellForRowAtIndexPath:
You should go for UITextView/UILabel with UIScrollView. UILabel is preferable if the content is not editable and you do not need the text scrolling on the fixed area.
If your needs fulfilled by these, you need to go with UILabel with UIScrollView.

Table cell label not wrapping last line in iOS7

I am getting very strange problem in my app. I am creating table view cell dynamically. And adjusting height of cell label according to content.
My problem is that my cell label is not showing last line after wrapping means if there are total three lines for my label then label shows only two lines. The code was working on iOS6 but on iOS 7 it is giving problem.
[labelCell.contentView bringSubviewToFront:labelCell.textLabel];
labelCell.textLabel.numberOfLines = 0;
labelCell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
NSString *fieldLabel = labelCell.textLabel.text;
CGSize textSize = [self sizeForLabelWithString:fieldLabel width:600.0f andIndexPath:indexPath];
float newHeight = textSize.height+22.0f;
labelCell.height = newHeight;
use this method:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *fieldLabel = labelCell.textLabel.text;
CGSize textSize = [fieldLabel sizeWithFont:[UIFont fontWithName:#"Georgia" size:17.0f] constrainedToSize:CGSizeMake(600, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
float newHeight = textSize.height+22.0f;
return newHeight;
}
Add below code to cellForRowAtIndexPath
UILabel *lblfield=[[UILabel alloc] init];
NSString *fieldLabel=[NSString stringWithFormat:#"%#",labelCell.textLabel.text];
CGSize textSize = [fieldLabel sizeWithFont:[UIFont fontWithName:#"Georgia" size:17.0f] constrainedToSize:CGSizeMake(600, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
float newHeight = textSize.height+22.0f;
lblfield.frame=CGRectMake(10, 0, 600, newHeight);
lblfield.backgroundColor=[UIColor clearColor];
lblfield.text=strusername;
[cell addSubview:lblfield];
I was having the same issue and just stumbled across this. My label was resizing to the correct size, but it wasn't showing the last line on any labels that wrapped to several lines. Moreover, when I selected/unselected the cell, I could suddenly see the last line.
I was able to resolve the issue by adding a small fudge factor (2 points) to the height of my cell. I updated the value to use for the height of the cell from:
return labelHeight + 2 * kTopAndBottomPadding;
to:
return labelHeight + 2 * kTopAndBottomPadding + 2.f;
You are changing the label height based on the content. So you need to handle the cell height dynamically or need to set the height of a cell based on the content(Max among the string.).
i.e
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return MAXStringHeght;
}
and try once.

Dynamic cell height iOS

I'm trying to dynamically recalculate the height of the cell that contain UITextView. The height of the cell depends on the height UITextView.
The source code below recalculate height for each cell in table. This is the some text from NSDictionary. After all heights has recalculated I reload my table view with new height that contain in array heightCells;
UITextView *tempTextView = [[UITextView alloc] init];
UIFont *font = [UIFont fontWithName:#"Helvetica" size:16.0];
[tempTextView setFrame:CGRectMake(10, 63, 300, 9999)];
[tempTextView setFont:font];
NSString *str = [d valueForKey:#"definition"]; // set long text
tempTextView.text = str;
CGRect frame = tempTextView.frame;
frame.size.height = tempTextView.contentSize.height + 20.0;
tempTextView.frame = frame;
[tempTextView sizeToFit];
int height = tempTextView.frame.size.height + 150.0; // set padding 150.0 px
[heightCells addObject:[NSNumber numberWithFloat:height]];
UITableView method:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return [[heightCells objectAtIndex:indexPath.row] floatValue];
}
On not retina display everything work good and the height recalculated correct, but on retina display I have problem with recalculation. I know that the retina and not retina have one 320x480 points view and it should not affect for recalculation. But in my case this is appear.
Please see screenshot below. As you can see on screenshot the bottom padding after share button a different for retina and not retina display. And I don't know which kind this problem.
Thanks for help!
It seems the tempTextView's frame differs when it is on the UITableViewCell. I described my working technique in this answer.

Resize UITableViewCell to UILabel's height dynamically

I want to resize cell's height according to the label's height and label's height according to text. Or is there any way I can resize the cell's height according to the text entered in UITextView?
THIS METHOD IS DEPRECATED SINCE iOS 7.0.
There is a UITableView delegate method called heightForRowAtIndexPath that is called before you create a cell or a table.
You could use the NSIndexPath passed to it to get the text at a specific row and use the sizeWithFont method from UIStringDrawing.h to compute a CGSize for that row.
For example:
CGSize size = [text sizeWithFont:font
constrainedToSize:maximumLabelSize
lineBreakMode:UILineBreakModeWordWrap];
And finally you would return size.height.
--For iOS7--
Basing this off of Josh Vera's answer … place this in heightForRowAtIndexPath.
I have my table data stored in an NSArray *tableData.
// set the desired width of the textbox that will be holding the adjusted text, (for clarity only, set as property or ivar)
CGFloat widthOfMyTexbox = 250.0;
-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Get a reference to your string to base the cell size on.
NSString *cellText = [self.tableData objectAtIndex:indexPath.row];
//set the desired size of your textbox
CGSize constraint = CGSizeMake(widthOfMyTextBox, MAXFLOAT);
//set your text attribute dictionary
NSDictionary *attributes = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14.0] forKey:NSFontAttributeName];
//get the size of the text box
CGRect textsize = [cellText boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];
//calculate your size
float textHeight = textsize.size.height +20;
//I have mine set for a minimum size
textHeight = (textHeight < 50.0) ? 50.0 : textHeight;
return textHeight;
}
I haven't tested it for iOS<7, but I believe it should work for that as well.

Resources