hi, i want to calculating no of lines when we give text in UITableviewcell?i have inserted text through UITextview which is in UITableview cell.but i want to increase the height of cell based on UItextview which is in UITableview cell.suppose if it has 4 lines ,one height...otherwise other height...i want to also calculate how many lines are in UITextview...and also set height at runtime?any help pls..?
To calculate the approximate size of your UITextView
based on a string, try NSString
's - (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(UILineBreakMode)lineBreakMode
method.
For example:
NSString *myText = @"Lorem ipsum dolor sit amet, consectetur adipisicing...";
CGSize newSize = [myText sizeWithFont:[textView font]
constrainedToSize:CGSizeMake([textView frame].size.width, MAXFLOAT)
lineBreakMode:UILineBreakModeWordWrap];
CGFloat newTextViewHeight = newSize.height;
CGFloat newTableViewCellHeight = newTextViewHeight + 42;
...where textView
refers to your UITextView
. (I've also assumed that the line break mode of your UITextView
is UILineBreakModeWordWrap
; if it isn't, make sure you change the appropriate code.)
You can calculate the fitted size of a UILabel based on the NSString content using the UIKitAdditions category on NSString. These methods include...
-sizeWithFont:
-sizeWithFont:constrainedToSize:
-sizeWithFont:constrainedToSize:lineBreakMode:
-sizeWithFont:forWidth:lineBreakMode:
-sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:
In my implementations of this pattern, I typically use the third one, -sizeWithFont:constrainedToSize:lineBreakMode:
.
The most efficient implementations of variable-height UITableViewCells will only calculate the size once per displayed string. This is typically an expensive operation and you definitely don't want expensive operations being performed frequently while scrolling - your scrolling FPS will drop through the floor. This means caching the calculated height somewhere, and for me that somewhere is the model object associated with the specific row.
Personally, I create a custom subclass of UITableViewCell whenever I'm implementing a variable-height table cell. I then create a class method that simply performs the sizeWithFont:constrainedToSize:lineBreakMode:
operation given a model object which will contain the string being displayed as a parameter. The table view controller implementation's of tableView:heightForRowAtIndexPath:
then looks something like this:
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath {
CGFloat height;
DataObject *rowData = [self.dataArray objectAtIndex:indexPath.row];
if (rowData.cachedHeight) {
height = [rowData.cachedHeight doubleValue];
} else {
height = [CustomTableViewCell heightForRowWithString:rowData.contentStr];
rowData.cachedHeight = [NSNumber numberWithDouble:height];
}
return height;
}
N.B. You could/should probably use a UILabel, rather than a UITextView, since you don't need the scrolling behavior of the UITextView.