This question already has answers here:
How do I wrap text in a UITableViewCell without a custom cell
(10 answers)
Closed 9 years ago.
I am trying to get a list of comments from database. The comments might be even up to 100lines. The problem is that i can't get it to break line. I've used
comment.adjustFontSizeToFitWidth = NO;
comment.numberOfLines = 0;
comment.lineBreakMode = UILineBreakModeCharacterWrap
Curently the test comment is:
looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong
but it ends at middle, no "..." and no text wrap. How to fix this?
Btw. there are lots of cells like this.
Do this thing in your tableview delegate :
- (CGFloat)tableView:(UITableView *)tableView1 heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGSize maximumSize = CGSizeMake(480.0,1000.0); // Put appropriate required height and width.
NSString *dateString = [NSString stringWithFormat:#"%#",yourString];
UIFont *dateFont = [UIFont fontWithName:#"Helvetica" size:14];
CGSize dateStringSize = [dateString sizeWithFont:dateFont
constrainedToSize:maximumSize
lineBreakMode:UILineBreakModeWordWrap];
return dateStringSize.height;
}
This code will set appropriate height for your cell. Then in your cellForRowAtIndexPath function. keep this code :
comment.adjustFontSizeToFitWidth = NO;
comment.numberOfLines = 0;
comment.lineBreakMode = UILineBreakModeCharacterWrap
You will have to impement tableView:heightForRowAtIndexPath:
See http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html for details.
Within that method you will have to calculate the height that each of the cells requries based on the height that the lable requires to display the text.
In the UIExtenstions of NSString you will find the helper methods for exactly that calculation. http://developer.apple.com/library/ios/#documentation/uikit/reference/NSString_UIKit_Additions/Reference/Reference.html
So is full with examples on how to use – sizeWithFont:constrainedToSize:lineBreakMode:.
Then you can either layout the cell items in cellForRowAtIndexPath where you need to use sizeWithFont:... again for the calculation of the size of the text. Or, if you want a neat solultion, better subclass UITableViewCell and overwrite its layoutSubviews method and do the layout there.
#define FONT_SIZE 11.0f
#define CELL_CONTENT_WIDTH 157.0f
#define CELL_CONTENT_MARGIN 10.0f
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *text = YorCommentString;// or any String that u want.
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); // set as u want
return height + (CELL_CONTENT_MARGIN * 2); // set as u want
}
In above code no need to set numberOfLines for UILabel.
Related
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 want to dynamicaly set the cell's rowheight to the UILabel's height (which is cell's subview).
The difficulty is UILabel needs to get its height by checking its string content
Two problems:
1- How can I set correctly the numberofLines in UILabel depending on the string lenght?
2- How can I reflect this UIlabel height to row.height because delegate method heightForRowAtIndexPath is called before the cellForRow method?
here is some code, whcih I am sure I do somethinsg wrong..
Nsstring st= "very long text...";
CGSize theSize = [st sizeWithFont:font constrainedToSize:CGSizeMake(250.0f, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
CGRect cg = CGRectMake(0.0f, 0.0f, 250.0f, theSize.height);
UILabel *label=[[UILabel alloc] initWithFrame:cg];
label.textAlignment=UITextAlignmentLeft;
label.text=st;
[label setNumberOfLines:10];// this is trouble!! how to set this!?
[cell addSubview:label];
- (CGFloat) tableView: (UITableView *) tableView heightForRowAtIndexPath: (NSIndexPath *) indexPath {
CGSize labelSize = CGSizeMake(200.0, 20.0);
if ([string length] > 0)
labelSize = [string sizeWithFont: [UIFont boldSystemFontOfSize: 17.0] constrainedToSize: CGSizeMake(labelSize.width, 1000) lineBreakMode: UILineBreakModeWordWrap];
return 24.0 + labelSize.height;
}
You need to implement heightForRowAtIndexPath. In my sample code, I returned 24 + the height of the label
(Mashup of my comments)
1- How can I set correctly the numberofLines in UILabel depending on the string lenght?
UILabel's numberOfLines is the maximum number of lines. "To remove any maximum limit, and use as many lines as needed, set the value of this property to 0."
2- How can I reflect this UIlabel height to row.height because delegate method heightForRowAtIndexPath is called before the cellForRow method?
You need to implement heightForRowAtIndexPath: and return the appropriate height for the row. You can move the computation of theSize to a helper method and use it from cellForRowAtIndexPath: too.
You can have a separate textForRow: function that you can both use to set the text of the label, and also to calculate the size of the label (and the cell) in the delegate method (much like you have already done)
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.
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.