im making a iphone app so that any text in the detailTextlabel will fit no matter what
for example...if the detailTextLabel is short it will show a normal size cell however it if is long it will show a bigger cell
thank you :D
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
CGSize maxSize = CGSizeMake(max_size);
CGSize cellSize = [yourString
sizeWithFont:[UIFont systemFontOfSize:15]
constrainedToSize:maxSize
lineBreakMode:UILineBreakModeWordWrap];
return cellSize.height;
}
Consider my implementation:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
switch (theSection) {
case 0:
if (indexPath.row ==6) //the row you need to check for height (if any)
{
if([yourString length]<=35) // if less than 35 chars, just return a basic size of 50
{
return 50;
}
CGSize detailSize = [yourString sizeWithFont:[UIFont systemFontOfSize:20]constrainedToSize:CGSizeMake(270, 4000)lineBreakMode:UILineBreakModeWordWrap];
return detailSize.height;
}
hope it helps, to help your understanding you may want to have a look at 'sizeWithFont' to understand what this is doing to return a size.
You have to use the tableView:heightForRowAtIndexPath: method on your UITableViewDelegate to return the height of the row. Note that this method should be very fast if you have more than a few rows, as it will be called for every row in the table rather than just the visible rows.
Related
I'm using a grouped uitableview. I have a lot of sections.
In the first section there is just one UITableViewCell. In this cell, I will show a text.
The problem is that I don't know the number of characters of the text and I would like to change the height of cell to show entire text.
can someone help me?
Thanks
in the UItableViewDelegate method - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath you can return height for specific row. For that you must know the length of the string you want to display in that row.
see this link Get tableView:heightForRowAtIndexPath: to happen after tableView:cellForRowAtIndexPath:?
You will have to use heightForRowAtIndexPath. Within that you will have to do all the calculations that you do in the cell's layoutsubviews or in cellForRowAtIndexPath too. For doing so you will have to figure out how long the text is. You will have to access the text in cellForRowAtIindexPath anyway. So why don't you know how long the text will be?
To determine the size of the actual text you can use NSSTring's sizeWithFont:forWidth:lineBreakMode:.
I have solved the problem applying the code below and creating a property numberOfTextRows.
(CGFloat)tableView:(UITableView *)t heightForRowAtIndexPath:(NSIndexPath *)indexPath
float height = 45.0f;
if (indexPath.section == 0 && indexPath.row == 0) {
CGSize theSize = [self.strResult sizeWithFont:[UIFont systemFontOfSize:12.0f] constrainedToSize:CGSizeMake(265.0f, 9999.0f) lineBreakMode:UILineBreakModeWordWrap];
self.numberOfTextRows = round(theSize.height / 12);
if ((indexPath.section == height) || (self.numberOfTextRows < 2)) {
height = 45;
} else {
height = theSize.height + 16;
}
}
return height;
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = self.numberOfTextRows;
I have surfed but not working any of the solution as per my requirement
Let me explain first ! I want to resize my label as per my text string ! I have referred some of stackoverflow questions !
Now look I have first string as "Hello First," This string completely fits with cell.
Now the problem with second string. Second string is "Hello Second, How are you ? Everythings all right ? Hows all there ?" This is too long string as per cell content. So I want to make my cell resizable as per string text !!
How can I ?
CGSize maximumSize = CGSizeMake(3000,3000);
UIFont *dateFont = [UIFont fontWithName:#"Arial" size:16];
CGSize dateStringSize = ["YOURString" sizeWithFont:dateFont
constrainedToSize:maximumSize
lineBreakMode:"Yourlable".lineBreakMode];
It will give "Width and height" .As per height you will fix your "heightForRowAtIndexpath" in table view cell(through dynamic)
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return dateStringSize.height;
}
(or)else
"set your lable height according to string height"
You need to implement this delegate method heightForRowAtIndexPath
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Load text in NSString which you want in Cell's LabelText.
NSString *cellText=[valueArray objectAtIndex:indexPath.row];
//define font for Labeltext...
UIFont *cellFont = [UIFont fontWithName:#"Helvetica" size:14.0];
CGSize constraintSize = CGSizeMake(330.0f, MAXFLOAT);
//sizeWithFont: Returns the size of the string if it were rendered with the specified constraints. So it will break your line according to font size and constraint size.
CGSize labelSize_val = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
return labelSize_val.height+20; // Add 20 in height so your UITableView will be neat and clean.
}
Note:
– sizeWithFont:constrainedToSize:lineBreakMode: is deprecated in iOS 7. Use boundingRectWithSize:options:attributes:context: instead.)
Instead of making dynamic size of your label, you can set it maximum (as in your thought) then set numberOfLines property of label. That will allow and move to next line when it reaches end of label. One more thing to set proper size of your cells. Only you've know the exact length of your data.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
In above function you should calculate the height of the cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
Here you should set proper frame and numberOfLines to zero to textLabel to accomodate the long string.
guys.
I have UITableView with different cells and I have code, which counts height. In one project it works perfect, but in second it returns height equals 0.
What could be causing this?
My code:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
CGFloat cellWidth = 320.0f;
CGSize size = [cell.textLabel.text sizeWithFont:cell.textLabel.font constrainedToSize:CGSizeMake(cellWidth, CGFLOAT_MAX) lineBreakMode:cell.textLabel.lineBreakMode];
CGFloat height = size.height;
NSLog(#"Height: %f", height);
return height;
}
The heightForRowAtIndexPath: delegate method gets called before the cellForRowAtIndexPath: delegate method. In your code you are calculating your cell height based on cell.textLabel.text, but the text in the cell has not been set yet.
You need to get the text to use in this method from somewhere else other than the table cell (presumably you can get it from wherever you are getting it from when you set the textlabel.text value in cellForRowAtIndexPath).
I am trying to create a table with cells that contain UITextViews inside them for variable amounts of text. I need to specify the height of each cell to match the content size of the textviews. I am using...
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
UITextView *historyNode = [[[UITextView alloc]init]autorelease];
historyNode.text = (#"%#",[globalArrayWithStrings objectAtIndex:[indexPath row]]);
NSLog(#"%2.0f",historyNode.frame.size.height);
return historyNode.contentSize.height;
}
For some reason, it always prints 0. If i print the height of a textview created in interface builder, it prints the correct value. Any ideas how to get around this or why I can't get the size of a textview until it is added to the view.
You can use the sizeWithFont: method to find the dynamic height. Your heightForRowAtIndexPath: should look like this.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *text = [globalArrayWithStrings objectAtIndex:[indexPath row]];
CGSize maxSize = CGSizeMake(textViewWidth, 999); // 999 can be any maxmimum height you want
CGSize newSize = [text sizeWithFont:aFont constrainedToSize:maxSize lineBreakMode:textViewLineBreakMode];
return newSize.height;
}
First set the frame of the uitextView and then you will get the height of the text view.
I want to make a UITableView which automatically adjust it's cell size by it's contents.
The contents are from XML file and the data is just text.
I want to put the text in a cell and it should be automatically does word-wrap and support multi-line.
So each cell can have different height. Is this possible?? or any idea?
Thank you ;)
Yes it is possible. Make use of the following method for doing it,
- (CGFloat)tableView:(UITableView *) tableView heightForRowAtIndexPath: (NSIndexPath *)indexPath {
CGSize cellHeight;
if (indexPath.row == 0) {
cellHeight = [yourXMLContents sizeWithFont:[UIFont systemFontOfSize:13.0f] constrainedToSize:CGSizeMake(300.0, 1000.0) lineBreakMode:UILineBreakModeWordWrap];
return cellHeight.height + 20;
} else
return 30;
}
Have you looked at the three20 library? It'll resize the cells to fit the content. In the very least, you can look at how the code is implemented to resize cells.