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)
Related
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.
I have a uitableview with uitableviewcells however some of my text is so long its going right to the edge of the cell, where my accessory tick view is going to be when selected..
I am currently wrapping the text if it extends beyond the width and it goes onto the second line.. However I was hoping there is a nice way to restrict the width of this UITableViewCell label.
Inside your cellForRowAtIndexPath: function. The first time you create your cell:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 0;
cell.textLabel.font = [UIFont fontWithName:#"Helvetica" size:17.0];
}
Now specify how large your UITableViewCell will be, so do that in your heightForRowAtIndexPath function:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *str = [[arrTexts objectAtIndex:indexPath.row]; // filling text in label
CGSize maximumSize = CGSizeMake(300, 100); // change width and height to your requirement
CGSize strSize = [str sizeWithFont:[UIFont fontWithName:#"Helvetica" size:17] constrainedToSize:maximumSize lineBreakMode:UILineBreakModeWordWrap] //dynamic height of string depending on given width to fit
return (10+strSize.height+10) // caculate on your bases as u have string height
}
In cellForRowAtIndexPath Set the width of the textLabel
cell.textLabel.frame = CGRectMake(cell.textLabel.frame.origin.x, cell.textLabel.frame.origin.y, 280, cell.textLabel.frame.size.height);
thats all.
I think you need subclass UITableViewCell to override -layoutSubviews... call super, then frame your label appropriately. Constraints my be of some help, but I haven't used them on iOS yet. (Assuming the problem is the built-in layout function make the label as wide as it needs to be to accommodate your text)
There are two ways to do so.
1) Create a custom UITableViewCell class, add your UILabels with whatever position & size you want. And then use it in cellForRowAtIndexPath method.
2) Else, don’t use default UILabels of UITableViewCell ie. textLabel & detailTextLabel, add your own UILabels in cell with whatever position & size you want.
I have a UILabel inside a UITableViewCell and I was trying to adjust the height, however when the height is greater than the cell height it overflows to the next cell below it. How can I avoid this? I am adding this into my contentView:
[self.contentView addSubview:self.commentsText_];
if you want to hide the overflows.
self.contentView.clipsToBounds = YES;
or you may want to layout by overwriting at
- (void)setNeedsLayout
{
[super setNeedsLayout];
self.commentsText_.frame = .... // layout your label
}
Using following code you can calculate height of label and also change the height of cell
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath {
UILabel *yourlabel;// use your memober class UILabel. I am declare here temporary.
CGSize s = [yourlabel.text sizeWithFont:[UIFont systemFontOfSize:15] // enter your text font size and cell-width
constrainedToSize:CGSizeMake(yourcellwidth, MAXFLOAT) // - 40 For cell padding
lineBreakMode:UILineBreakModeWordWrap];
return s.height; //this will give you height of UILabel view you can change using addition according your requirements
}
Hope, this will help you..
I'm loading my UITextView from an XML feed so the text is constantly changing. I'm trying the following to resize the cell and text, and it resizes the cell but not the text view, it's just not displaying the text view, or sometimes just part of it.
Any tips along the right way will be really appreciated;
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
AssessObject *newObj1;
newObj1=[totalArray objectAtIndex:indexPath.row];
NSString *cellText = newObj1.routeText;
UIFont *cellFont = [UIFont fontWithName:#"Helvetica" size:16.0];
CGSize constraintSize = CGSizeMake(188.0, CGFLOAT_MAX);
CGSize textViewSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
return textViewSize.height + 200;
}
Check the AutoresizingMask of the UITextView you have added to your cell.
Make sure it is set so that it resizes with the cell (you can do this either in IB, or via code using the UIViewAutoresizingMaskFlexibleWidth|UIViewAutoresizingMaskFlexibleWidth value)
Set the textView size equal to textView's contentSize.
Something like this:
CGRectMake(textView.frame.origin.x, textView.frame.origin.y, textView.frame.size.width, textView.contentSize.height);
I'm making the height of the textView equal to the height of it's contentView.
setup the font size, text content and frame rect of the UITextView, then [UITextView sizeToFit] to calculate the contentSize of UITextView, then calculate the row height with the size of contentSize.
Don't forget to resize the frame rect of UITextView;
I have used Bruno's idea to resize my TextView according to the amount of text, when I put it to the ScrollView. This is how I do this. A bunch of constants there, that you may not use. It is important to resize textView after adding it to the ScrollView.
// Programmatic creation of scroll view layout
NSString *text = #"Your text";
CGFloat textOffSetInColumn = 10;
CGFloat infoTextWidth = 196;
CGFloat infoOffsetVertical = 36;
CGFloat initialTextHeight = 50;
// Create textView with initial height
CGRect infoTextFrame = CGRectMake(textOffSetInColumn, infoOffsetVertical, infoTextWidth, initialTextHeight);
infoTextView = [[UITextView alloc] initWithFrame:infoTextFrame];
infoTextView.text = text;
[scrollView addSubview:infoTextView];
// Resize textView
CGFloat infoTextHeight = infoTextView.contentSize.height;
infoTextFrame = CGRectMake(textOffSetInColumn, infoOffsetVertical, infoTextWidth, infoTextHeight);
infoTextView.frame = infoTextFrame;
If you want to change the size of TextView and center it to the previous center, you can use this code:
// Changing size of TextView and centering
CGPoint center = self.categoryTextView.center;
self.categoryTextView.frame = CGRectMake(_categoryTextView.frame.origin.x, _categoryTextView.frame.origin.y, _categoryTextView.frame.size.width, _categoryTextView.contentSize.height);
self.categoryTextView.center = center;
Instead of categoryTextView use your own Outlet name.
I'm tried to display the Title Data in the Tableview Cell it displayed but partially (...) i want to display full title in the table cell may be in two or three line i used customize cell and use the label and set label property but still data displayed partially
titleLabel.text = aNewsInfo.title;
Can any one suggest me the way to overcome this prob...
Thanks in Advance...
Try this code:
myLabel.lineBreakMode = UILineBreakModeWordWrap;
myLabel.numberOfLines = 2; // 2 lines ; 0 - dynamical number of lines
myLabel.text = #"Lorem ipsum dolor sit\namet...";
Maybe you can try:
[titleLabel sizeToFit];
And the you need to adjust the height of the cell with:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
The label's property needs to be set
adjustsFontSizeToFitWidth
A Boolean value indicating whether the font size should be reduced in order to fit
the title string into the label’s bounding rectangle.
#property(nonatomic) BOOL adjustsFontSizeToFitWidth
And
numberOfLines
The maximum number of lines to use for rendering text.
#property(nonatomic) NSInteger numberOfLines
Discussion
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.
If you constrain your text using this property, any text that does not fit within the maximum number of lines and inside the bounding rectangle of the label is truncated using the appropriate line break mode.
When the receiver is resized using the sizeToFit method, resizing takes into account the value stored in this property. For example, if this property is set to 3, the sizeToFit method resizes the receiver so that it is big enough to display three lines of text.
Hi try to add label in your cellforRow method
CGSize labelsize;
UILabel *commentsTextLabel = [[UILabel alloc] init];;
commentsTextLabel.tag =50;
[commentsTextLabel setNumberOfLines:0];
[commentsTextLabel setBackgroundColor:[UIColor clearColor]];
NSString *text=[#"dasdasdasdasfasfasfasfasfasfsafasfsafasfasfasfasfsafsafasfasfsaf"];
[commentsTextLabel setFont:[UIFont fontWithName:#"Helvetica"size:14]];
labelsize=[text sizeWithFont:commentsTextLabel.font constrainedToSize:CGSizeMake(268, 2000.0) lineBreakMode:UILineBreakModeWordWrap];
commentsTextLabel.frame=CGRectMake(10, 24, 268, labelsize.height);
[cell.contentView addSubview:commentsTextLabel];
[commentsTextLabel release];
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGSize labelsize;
UILabel * textDesc1 = [[UILabel alloc] init];
[textDesc1 setNumberOfLines:0];
textDesc1.text=[#"dasdasdasdasfasfasfasfasfasfsafasfsafasfasfasfasfsafsafasfasfsaf"];
[textDesc1 setFont:[UIFont fontWithName:#"Helvetica" size:14.0]];
labelsize=[textDesc1.text sizeWithFont:textDesc1.font constrainedToSize:CGSizeMake(268, 2000.0) lineBreakMode:UILineBreakModeWordWrap];
labelsize.height=labelsize.height+35;
[textDesc1 release];
return (CGFloat)labelsize.height;
}