I would really appreciate it if you could tell me how I could set the height of a UITableViewCell depending on the height of a UILabel.
My current code to set the UILabel's height is:
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 0;
cell.textLabel.text = [self getItemForKey:kSummary];
cell.textLabel.font = [UIFont systemFontOfSize:15];
cell.textLabel.textColor = [UIColor colorWithRed:54.0f/255.0f green:54.0f/255.0f blue:54.0f/255.0f alpha:1.0f];
CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
CGSize labelSize = [[cell.textLabel text] sizeWithFont:[cell.textLabel font] constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
cell.textLabel.frame = CGRectMake( 0, 0, 280, labelSize.height);
You will need to implement the UITableViewDelegate method - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *text = [self getItemForKey:kSummary];
CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
//You will need to define kDefaultCellFont
CGSize labelSize = [text sizeWithFont:kDefaultCellFont
constrainedToSize:constraintSize
lineBreakMode:UILineBreakModeWordWrap];
return labelSize.height + ANY_OTHER_HEIGHT;
}
Related
I need to create a cell height according to lable's content with orientation support curretly have tried this and working fine for portrait but not in landscape so need solution which works on both so if any one can please help me.
thanks in advance for your efforts.
this is my code in tableview
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSArray *arr = [arrData objectAtIndex:tableView.tag];
NSString *text = [arr objectAtIndex:indexPath.row];
CGSize constraint = CGSizeMake(contentWidth - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:20] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
CGFloat height = MAX(size.height, 44.0f);
return height + (CELL_CONTENT_MARGIN * 2);
}
// Customize the appearance of table view cells.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
UILabel *label = nil;
cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
cell.accessoryView = nil;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
label = [[UILabel alloc] initWithFrame:CGRectZero];
[label setLineBreakMode:NSLineBreakByWordWrapping];
[label setNumberOfLines:0];
[label setFont:[UIFont systemFontOfSize:20]];
[label setTag:1];
[label setBackgroundColor:[UIColor redColor]];
[[cell contentView] addSubview:label];
}
NSArray *arr = [arrData objectAtIndex:tableView.tag];
NSString *text = [arr objectAtIndex:indexPath.row];
CGSize constraint = CGSizeMake(contentWidth - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:20] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
if (!label)
label = (UILabel*)[cell viewWithTag:1];
[label setText:text];
[label setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, contentWidth - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))];
return cell;
}
In heightForRowAtIndexPath you can set height according to orientation.
when orientation change, reload cells. In heightForRowAtIndexPath use this value to check orientation and while this method is called -(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration reload your cells.
So your heightForRowAtIndexPath will be somethign like this
if(UIInterfaceOrientationIsPortrait(orientation)){
}
else
{
}
Make sure you keep your cells subviews as autoresizing flexible.
I have a basic UITableView, that I fill with a web service online, but I can't find a way to set the height of my cells (dynamic numbers of cells) according to the height of my textView.
Here's how I fill my cell :
UITextView *textView = (UITextView *)[cell viewWithTag:106];
textView.text = [[stories objectAtIndex:indexPath.row] objectForKey:#"texte"];
in my cellForRow methods :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
I tried this but get an error of EXC_BAD_ACCESS
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"PerleCell"];
UITextView *textView = (UITextView *)[cell viewWithTag:106];
textView.text = [[stories objectAtIndex:indexPath.row] objectForKey:#"texte"];
CGFloat *heightCell = (CGFloat*)textView.text.length;
return *heightCell;
}
In following method you can change height of your cell according to your textView.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
// Here check length of your textView
// and after getting length return that length;
NSString *myString = [[stories objectAtIndex:indexPath.row] objectForKey:#"texte"];
CGSize maximumSize = CGSizeMake(300, 9999);
UIFont *myFont = [UIFont fontWithName:#"Helvetica" size:14];
CGSize myStringSize = [myString sizeWithFont:myFont constrainedToSize:maximumSize lineBreakMode:UILineBreakModeWordWrap];
return size.height;
}
Also in cellForRowAtIndexPath method calculate width of string in same manner and then give frame of width of yourtextView accordingly. After that add your textview on cell's contentView.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// Create cell here by using any style and for this you can google some what that how to create cell in UITableView
// Here check length of your textView
// and after getting length return that length;
NSString *myString = [[stories objectAtIndex:indexPath.row] objectForKey:#"texte"];
CGSize maximumSize = CGSizeMake(300, 9999);
UIFont *myFont = [UIFont fontWithName:#"Helvetica" size:14];
CGSize myStringSize = [myString sizeWithFont:myFont constrainedToSize:maximumSize lineBreakMode:UILineBreakModeWordWrap];
UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(0,0,300,size.height)];
textView.text = myString;
[cell.contentView addSubView:textView];
return cell;
}
I have a grouped tableview with a right detail cell layout. Now I got my data back from my server.
Sometimes it happens that I got back a very large text. So the tableviewCell should be higher than the others. You can see the screenshot of my problem over here.
Like you can see in my first cell I have a very large text. Now I want that the height of the cell grows with the length of the text, so i can always see the entire text instead off the '...' This is what I have in code at te moment.
#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;
}
EDIT
Code I have at the moment.
- (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"];
UIFont *cellFont = [UIFont fontWithName:#"Helvetica" size:14.0];
CGSize constraintSize = CGSizeMake(330.0f, 400); // Make changes in width as per your label requirement.
CGSize textSize = [text sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
return textSize.height;
}
And this is the result.
EDIT CellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Subscription *sub = [_dictTask valueForKeyPath:#"subscription"];
NSArray *meta = sub.meta.allObjects;
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = [[meta objectAtIndex:indexPath.row]valueForKey:#"sum_label"];
cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.detailTextLabel.numberOfLines = 0;
cell.detailTextLabel.text = [[meta objectAtIndex:indexPath.row]valueForKey:#"sum_value"];
cell.textLabel.textColor = [UIColor colorWithRed:102/255.0
green:102/255.0
blue:102/255.0
alpha:1.0];
cell.textLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:18];
return cell;
}
You need to use lineBreakMode:UILineBreakModeWordWrap in your code.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *text = [[meta objectAtIndex:indexPath.row]valueForKey:#"sum_value"];
UIFont *cellFont = [UIFont fontWithName:#"Helvetica" size:14.0];
CGSize constraintSize = CGSizeMake(330.0f, MAXFLOAT); // Make changes in width as per your label requirement.
CGSize textSize = [text sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
return textSize.height;
}
Moreover you need to set two properties of detailTextLabelalso in datasource methodcellForRowAtIndexPath` like this :
- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.detailTextLabel.numberOfLines = 0;
}
numberofLines property describes:
This property controls the maximum number of lines to use in order to fit the label’s text into its bounding rectangle. The default value for this property is 1. To remove any maximum limit, and use as many lines as needed, set the value of this property to 0.
Try this:
- (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"];
UIFont *cellFont = [UIFont fontWithName:#"Helvetica-Bold" size:18.0];
CGSize constraintSize = CGSizeMake(330.0f, MAXFLOAT); // Make changes in width as per your label requirement.
CGSize textSize = [text sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
return textSize.height + 20; }
Then, consider using unique cell identifiers. The idea here is that reusing the cell will result in mismatched heights for the contents. (Some people don't like the idea of unique identifiers, but they work and they're not problematic unless your table is going to be huge.) Accordingly, try this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Subscription *sub = [_dictTask valueForKeyPath:#"subscription"];
NSArray *meta = sub.meta.allObjects;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:#"%i", indexPath.row]];
if (cell == nil){
cell.textLabel.text = [[meta objectAtIndex:indexPath.row]valueForKey:#"sum_label"];
cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.detailTextLabel.numberOfLines = 0;
cell.detailTextLabel.text = [[meta objectAtIndex:indexPath.row]valueForKey:#"sum_value"];
cell.textLabel.textColor = [UIColor colorWithRed:102/255.0
green:102/255.0
blue:102/255.0
alpha:1.0];
cell.textLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:18];
}
return cell;
}
I need to resize the cell height based on the content size/length.tried several methods, which one gives the exact height without overlapping?
see this tutorial for change UITableViewCell Height Dynamically..
Resizing-A-UITableViewCell
and also use this tutorial..
uitableviewcell-dynamic-height
also use tableView:heightForRowAtIndexPath: method for set height of cell with indexpath
This is from my previous answer - Customizing UITableViewCell's height:
Use this method to get the text height of the text
-(CGFloat)getLabelHeightForText:(NSString *)text andWidth:(CGFloat)labelWidth
{
CGSize maximumSize = CGSizeMake(labelWidth, 10000);
//provide appropriate font and font size
CGSize labelHeighSize = [text sizeWithFont: [UIFont fontWithName:#"Trebuchet MS" size:13.0f]
constrainedToSize:maximumSize
lineBreakMode:UILineBreakModeTailTruncation];
return labelHeighSize.height;
}
This method will return the height of the text you are passing. Add this method in your class. And use the tableView:heightForRowAtIndexPath: delegate method to set the height for each cell
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
Feedback *item = [self.items objectAtIndex:indexPath.row];
CGFloat textHeight = [self getLabelHeightForText:item.comment andWidth:162];//give your label width here
return textHeight;
}
Sample you can edit and try
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *itemAtIndex = (NSDictionary *)[chatQuestions objectAtIndex:indexPath.row];
if ([self sizeForText:[itemAtIndex objectForKey:#"text"]].height+20>50) {
return [self sizeForText:[itemAtIndex objectForKey:#"text"]].height+20;
}
else{
return 50;}
}
-(CGSize)sizeForText:(NSString*)text
{
CGSize constraintSize;
constraintSize.width = 190.0f;
constraintSize.height = MAXFLOAT;
UIFont *labelFont = [UIFont fontWithName:#"Noteworthy-Light" size:18];
CGSize stringSize =[text sizeWithFont:labelFont constrainedToSize: constraintSize lineBreakMode: UILineBreakModeWordWrap];
return stringSize;
}
//Calculate the expected size based on the font and linebreak mode of your label
CGSize maximumLabelSize = CGSizeMake(296,9999);
CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font constrainedToSize:maximumLabelSize lineBreakMode:yourLabel.lineBreakMode];
//adjust the label the the new height.
CGRect newFrame = yourLabel.frame;
newFrame.size.height = expectedLabelSize.height;
yourLabel.frame = newFrame;
In your case you need to set the height of cell based on the label.
Check the link :
http://dcraziee.wordpress.com/2013/05/22/calculate-size-of-uillabel-base-on-text-in/
There is a function named
-(CGFloat)getHeightForLabel:(NSString *)_str font:(UIFont *)fontOfObject
Use that function to calculate height as :
-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat _height = [self getHeightForLabel:self.label.text font:[self.label font]];
return _height;
}
im having troubles making my UILabel height dynamic. The label is contained in a custom TableViewCell
I have managed to make the TableViewCellHeight dynamic, but i cant make the label multiple lines.
Whenever i set number of lines = 0 (in IB as well as code), the label/text disappears completely. And lineBreakMode doesnt do much either?
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
Book *eventUp = [appDelegate.eventDataUp objectAtIndex:indexPath.row];
NSString *text = eventUp.date;
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);
//return 66;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [appDelegate.eventDataUp count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"EventUpCVCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
self.eventUpCVCell = nil;
[[NSBundle mainBundle] loadNibNamed:#"EventUpCVCell" owner:self options:nil];
cell = self.eventUpCVCell;
label = [[UILabel alloc] initWithFrame:CGRectZero];
[label setLineBreakMode:UILineBreakModeWordWrap];
[label setMinimumFontSize:FONT_SIZE];
[label setNumberOfLines:0];
[label setFont:[UIFont systemFontOfSize:FONT_SIZE]];
[label setTag:1];
[[cell contentView] addSubview:label];
}
Book *eventUp = [appDelegate.eventDataUp objectAtIndex:indexPath.row];
UILabel *lblMonth = (UILabel *) [cell viewWithTag:kLabel1Tag];
UILabel *lblTitle = (UILabel *) [cell viewWithTag:kLabel2Tag];
UILabel *lblDate = (UILabel *) [cell viewWithTag:kLabel3Tag];
//month.text = eventUp.month;
lblTitle.text = eventUp.title;
lblDate.text = eventUp.date;
//lblDate.lineBreakMode = UILineBreakModeWordWrap;
lblDate.numberOfLines = 1;
lblMonth.text = [NSString stringWithFormat:#"%# ", eventUp.month];
//cell.textLabel.text = eventUp.title;
//cell.detailTextLabel.text = eventUp.date;
//NSLog(#"Hello");
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// Set up the cell
return cell;
}
You have to set the label frame after creating it, actually you create it with CGRectZero Frame
try:
label = [[UILabel alloc] initWithFrame:CGRectZero];
[label setLineBreakMode:UILineBreakModeWordWrap];
[label setMinimumFontSize:FONT_SIZE];
[label setNumberOfLines:0];
[label setFont:[UIFont systemFontOfSize:FONT_SIZE]];
[label setTag:1];
Book *eventUp = [appDelegate.eventDataUp objectAtIndex:indexPath.row];
NSString *text = eventUp.date;
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
[label setFrame:CGRectMake(0,0,size.width,size.height)];