I have read alot about dynamic height for UITableViewCell and for the life of me I cannot get it working.
I have a uilabel with dynamic content within a dynamic cell.
I am using the storyboard and have the constraints as so :
I am populating the table using
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"labelCell";
detailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[detailTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.dynamicLabel.text = sectionString;
return cell;
}
I do not know why everything I have tried has failed. I am thinking it may be the connections on the uiLabel?
This is it , Right :
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row == 0) {
NSString *text = myText;// [myArray objectAtIndex:indexPath.row];
CGSize size;
if (SYSTEM_VERSION_LESS_THAN(#"7.0")) {
// code here for iOS 5.0,6.0 and so on
CGSize fontSize = [text sizeWithFont:[UIFont fontWithName:#"Helvetica" size:17]];
size = fontSize;
}
else {
// code here for iOS 7.0
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:#"Helvetica Neue" size:19], NSFontAttributeName,
nil];
CGRect fontSizeFor7 = [text boundingRectWithSize:CGSizeMake(571, 500)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributesDictionary
context:nil];
size = fontSizeFor7.size;
}
NSLog(#"indexPAth %ld %f",(long)indexPath.section, size.height);
return size.height +30 ;
}
Finally, at the delegate of cellForRowAtIndexPath: don't forget to make the UILabel flexible for text :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// ........
cell.dynamicLabel.text = sectionString;
cell.dynamicLabel.numberOfLines = 0;
return cell;
}
Related
uitableview autolayout custom cell causing extra height. Unable to detect where causing the issue while in different project working fine.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
[_stubCell layoutSubviews];
[self configureCell:_stubCell atIndexPath:indexPath];
CGFloat height = [_stubCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
return height;
}
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 40.f;
}
- (void)configureCell:(CCACustomCell *)cell atIndexPath:(NSIndexPath *)indexPath {
cell.customLabel.text = [_tableData objectAtIndex:indexPath.row];
cell.customLabel.font = [UIFont fontWithName:#"Avenir-Light" size:16.0];
cell.customLabel.textColor = UIColorFromRGB(0x32383B);
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CCACustomCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
Could you provide details on how are you creating _stubCell?It might be due to some constraint on label.
Have you tried with calculating string height?
+(CGFloat )calculateStringHeight:(NSString *)string WithFont:(UIFont *)font andSidePadding:(CGFloat )padding{
CGRect rect = [string boundingRectWithSize:CGSizeMake([[UIScreen mainScreen]bounds].size.width - (2*padding), 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:#{NSFontAttributeName:font} context:nil];
return rect.size.height;
}
You can return required height for your Label like this. Where you should pass Your description label width in LABEL_DESCRIPTION_WIDTH.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGSize constrainedSize = CGSizeMake(LABEL_DESCRIPTION_WIDTH , 9999);
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:FONT_NAME size:FONT_SIZE], NSFontAttributeName,nil];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:[_tableData objectAtIndex:indexPath.row] attributes:attributesDictionary];
CGRect requiredHeight = [string boundingRectWithSize:constrainedSize options:NSStringDrawingUsesLineFragmentOrigin context:nil];
return requiredHeight;
}
In my UIViewController I have a UITableView with custom cell.
Please check below image for screenshot.
In that I am giving "multi line " feature to UILabel (The selected label in screenshot) but I have 2 problems now.
When I open this viewcontroller I can see lots of blank space if my UILabel (Description label) have very small text. How can I manage these white spaces?
Please check the app screenshots
My code
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = #"cell";
InfoCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[InfoCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier];
}
cell.header.text=[names objectAtIndex:indexPath.row];
cell.details.text=[details objectAtIndex:indexPath.row];
[cell.header sizeToFit];
[cell.details sizeToFit];
return cell;
}
Please help to clear these issues
How to make UILabel text size dynamic instead of fixed multiline?
How to make UITableViewCell dynamic depends on its contents height?
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellText = #“Your text”;
UIFont *cellFont = [UIFont systemFontOfSize:YourFontSize];
CGSize constraintSize = CGSizeMake(TextviewWidth, MAXFLOAT);
CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
NSLog(#"labelSize : %f", labelSize.height);
return labelSize.height;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #“CustomCell”;
CommentCell *cell = (CommentCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
NSString *cellText = #“LabelText”;
UIFont *cellFont = [UIFont systemFontOfSize:yourFontSize];
CGSize constraintSize = CGSizeMake(LabelWidth, MAXFLOAT);
CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
cell.lbl.frame = CGRectMake(cell.lbl.frame.origin.x, cell.lbl.frame.origin.y, cell.lbl.frame.size.width, labelSize.height);
cell.lbl.lineBreakMode = NSLineBreakByWordWrapping;
}
This work for me.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *strYourText = #"Testing Case ";
NSMutableParagraphStyle *objPS = [[NSMutableParagraphStyle alloc] init];
objPS.lineBreakMode=NSLineBreakByWordWrapping;//here you have to pass the Line break mode which you have to label
CGSize labelSize = CGSizeMake(200.0, 1000.0);///width: label width and set the height 1000.0
CGSize textSizeForLabel = [strYourText boundingRectWithSize:labelSize options:NSStringDrawingUsesLineFragmentOrigin attributes:#{NSParagraphStyleAttributeName : objPS,NSFontAttributeName : [UIFont systemFontOfSize:18.0]} context:nil].size;
float labelHeight =ceilf(textSizeForLabel.height);//this are the only lable height.if there are other UIControls then add their heights and retuns.
//Like if One More Buttons is there in cell then return the button height with dynamic lable height (e.g. return labelHeight+buttonHeight );
//herer we are consider there is a only one label is there so return label height
return labelHeight;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = #"cell";
InfoCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[InfoCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier];
}
cell.header.numberOfLines = 0;
cell.header.lineBreakMode=NSLineBreakByWordWrapping;
NSString *strYourText = #"Testing Case ";
NSMutableParagraphStyle *objPS = [[NSMutableParagraphStyle alloc] init];
objPS.lineBreakMode=NSLineBreakByWordWrapping;//here you have to pass the Line break mode which you have to label
CGSize labelSize = CGSizeMake(cell.header.frame.size.width, 1000.0);///width label width and set the height 1000.0
CGSize textSizeForLabel = [strYourText boundingRectWithSize:labelSize options:NSStringDrawingUsesLineFragmentOrigin attributes:#{NSParagraphStyleAttributeName : objPS,NSFontAttributeName : [UIFont systemFontOfSize:18.0]} context:nil].size;
float labelHeight =ceilf(textSizeForLabel.height);
[cell.header setFrame:CGRectMake(cell.header.frame.origin.x, cell.header.frame.origin.x, cell.header.frame.origin.x, labelHeight)];
cell.header.text=strYourText;
return cell;
}
I would like the UITableView's row height to respond to changes in the user's preferred text size. For example, when the preferred text size increases, I would like to increase the row height by a proportional amount. Here's what I have so far:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.tableView reloadData];
// observe when user changes preferred text size
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(preferredContentSizeChanged:) name:UIContentSizeCategoryDidChangeNotification object:nil];
}
- (void)preferredContentSizeChanged:(NSNotification *)notification
{
[self.tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
// leverage Text Kit's Dynamic Type
cell.textLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
cell.textLabel.text = #"All work and no play...";
return cell;
}
So what's the best way to calculate a row height that reflects the user's preferred text size?
I have recently accomplished this and found it to be very simple. Instead of using sizeWithFont: you should use the new boundingRectWithSize:options:attributes:context method in iOS 7.
Set up your table view cell as usual and specify the preferredFontForTextStyle: on your text label as such:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TableViewCellIdentifier forIndexPath:indexPath];
//set your table view cell content here
[[cell textLabel] setText:#"Lorem ipsum dolour sit amet."];
[[cell textLabel] setNumberOfLines:0];
[[cell textLabel] setLineBreakMode:NSLineBreakByWordWrapping];
[[cell textLabel] setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleBody]];
return cell;
}
Then to correctly determine the size of the text label, evaluate the boundingRectWithSize:options:attributes:context to calculate the required height.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//add your table view cell content here
NSString *string = #"Lorem ipsum dolor sit amet.";
NSDictionary *attributes = #{NSFontAttributeName: [UIFont preferredFontForTextStyle:UIFontTextStyleBody]};
CGRect frame = [string boundingRectWithSize:CGSizeMake(CGRectGetWidth(tableView.bounds), CGFLOAT_MAX) options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading) attributes:attributes context:nil];
return ceilf(CGRectGetHeight(frame);
}
You may want to subclass your table view cell also to listen for UIContentSizeCategoryDidChangeNotification notifications at which point you can update your UI when the user changes their preferences in Settings.app
- (void)contentSizeCategoryDidChangeNotificationHandler:(NSNotification *)notification
{
[[self textLabel] setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleBody]];
}
Should you require additional padding around the text label, you can define a constant value such as
static CGFloat const TableViewCellPadding = 10.0;
With this in place, you can either add a constant value to the value returned from tableView:heightForRowAtIndexPath:
return (ceilf(CGRectGetHeight(frame) + TableViewCellPadding);
Or you could inset the frame returned from boundingRectWithSize:options:attributes:context as such:
CGRect frame = [string boundingRectWithSize:CGSizeMake(CGRectGetWidth(tableView.bounds), CGFLOAT_MAX) options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading) attributes:attributes context:nil];
frame = CGRectInset(frame, 0.0, TableViewCellPadding);
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([UIScreen mainScreen].bounds.size.width-20, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
float newHeight = textSize.height+22.0f;
return newHeight;
}
Add below code to cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
UILabel *lblfield=[[UILabel alloc] init];
NSString *fieldLabel=[NSString stringWithFormat:#"%#",labelCell.textLabel.text];
CGSize textSize = [fieldLabel sizeWithFont:[UIFont fontWithName:#"Georgia" size:17.0f] constrainedToSize:CGSizeMake([UIScreen mainScreen].bounds.size.width-20, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
float newHeight = textSize.height+22.0f;
lblfield.frame=CGRectMake(10, 0, [UIScreen mainScreen].bounds.size.width, newHeight);
lblfield.backgroundColor=[UIColor clearColor];
lblfield.text=strusername;
[cell addSubview:lblfield];
return cell;
}
For some strange reason the TableView Cells are not fitting the dynamic textual content properly. They should have a slight margin between the top and bottom of cell. Unfortunately, depending on the amount of textual content, some cells have no top and bottom margin(tightly packed) and others have huge margins.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
float result = 30;
if (indexPath.section != 0){
CGSize size = [[self textForIndexPath:indexPath isTitle:NO] sizeWithFont:kITDescriptionFont constrainedToSize:CGSizeMake(220, CGFLOAT_MAX) lineBreakMode:UILineBreakModeWordWrap];
result = MAX(result, size.height);
}
return result;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *doubleCellIdentifier = #"ITDoubleDetailCEllIdentifier";
static NSString *cellIdentifier = #"ITDetailCEllIdentifier";
NSString *cellIdent = (indexPath.section == 0)? doubleCellIdentifier : cellIdentifier;
ITDescriptionCell *cell = (ITDescriptionCell *)[tableView dequeueReusableCellWithIdentifier:cellIdent];
if (!cell) {
UITableViewCellStyle style = (indexPath.section == 0)? UITableViewCellStyleValue2 : UITableViewCellStyleDefault;
cell = [[ITDescriptionCell alloc] initWithStyle:style reuseIdentifier:cellIdent];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell.textLabel setNumberOfLines:0];
[cell.textLabel setFont:kITDescriptionFont];
}
else {
[cell.textLabel setText:#""];
[cell.detailTextLabel setText:#""];
}
[cell sizeToFit];
return cell;
}
You need to dynamically calculate the height of your text and based on that and the padding you want, adjust the height of the cell through the following delegate method:
#define PADDING 10.0f
- (CGFloat)tableView:(UITableView *)t heightForRowAtIndexPath:(NSIndexPath *)indexPath {
Subscription *sub = [_dictTask valueForKeyPath:#"subscription"];
NSArray *meta = sub.meta.allObjects;
NSString *text = [[meta objectAtIndex:indexPath.row] valueForKey:#"sum_value"];
CGSize textSize = [text sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:CGSizeMake(self.tableView.frame.size.width - PADDING * 3, 1000.0f)];
return textSize.height + PADDING * 3;
}
I am having a tableview with different heights for each cell based on the size of the text.
I calculate the rowsize in the delegate method -tableView:heightForRowAtIndexPath, and assign to the text in the datasource method -tableView:cellForRowAtIndexPath.
I am able to realize the appropriate height based on the text, but am not able to make the text wrap around; instead it just disappears off the screen. I am using a navigation-based app to generate the tableview in the child view controller using a nib. I played around with some of the options for tableview, but I can't make the text wrap. I would like to see the next wrap as if it were a textview, but I want the entire content displayed in the cell, without scroll bars in the table cell itself. Is this possible?
Here is the updated code, the height is adjusting itself correctly, but the cell is not word wrapping...
-
(CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *string = [self.quizDictionary objectForKey:[_temp objectAtIndex:testKV]];
CGSize stringSize = [string sizeWithFont:[UIFont boldSystemFontOfSize:15]
constrainedToSize:CGSizeMake(320, 9999)
lineBreakMode:UILineBreakModeWordWrap];
return stringSize.height+25;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
int row = indexPath.row;
if (row > 6) {
return nil;
}
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier]
autorelease];
}
// configure the cell...
NSString *string = [self getQuestionPart];
CGSize stringSize = [string sizeWithFont:[UIFont boldSystemFontOfSize:15] constrainedToSize:CGSizeMake(320, 9999) lineBreakMode:UILineBreakModeWordWrap];
UITextView *textV=[[UILabel alloc] initWithFrame:CGRectMake(5, 5, 290, stringSize.height+10)];
textV.font = [UIFont systemFontOfSize:15.0];
textV.text=string;
textV.textColor=[UIColor blackColor];
// textV.editable=NO;
[cell.contentView addSubview:textV];
[textV release];
testKV++;
return cell;
}
You want:
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
to get your text to wrap within the cell. This in combination with a careful implementation of tableView:heightForRowAtIndexPath: should do the trick.
Yes it is possible!!!
Looking at this,you need to be somewhat tricky. You need to calculate the height of the textView dynamically and based on the Height of the TextView,you need to return the Height for the cell..
This is the code by which you can calculate the size of string....
First get the size of String & set the height of the cell based on string
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *d=(NSDictionary *)[self.menuArray objectAtIndex:indexPath.section];
NSString *label = [d valueForKey:#"Description"];
CGSize stringSize = [label sizeWithFont:[UIFont boldSystemFontOfSize:15]
constrainedToSize:CGSizeMake(320, 9999)
lineBreakMode:UILineBreakModeWordWrap];
return stringSize.height+25;
}
- (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] autorelease];
//}
NSDictionary *d=(NSDictionary *)[self.menuArray objectAtIndex:indexPath.section];
NSString *string = [d valueForKey:#"Description"];
CGSize stringSize = [string sizeWithFont:[UIFont boldSystemFontOfSize:15] constrainedToSize:CGSizeMake(320, 9999) lineBreakMode:UILineBreakModeWordWrap];
UITextView *textV=[[UILabel alloc] initWithFrame:CGRectMake(5, 5, 290, stringSize.height+10)];
textV.font = [UIFont systemFontOfSize:15.0];
textV.text=string;
textV.textColor=[UIColor blackColor];
textV.editable=NO;
[cell.contentView addSubview:twitLbl];
[twitLbl release];
return cell;
}