Reputation: 2211
I want to set my label to have a fixed width in the UITableViewCell
so that my text can be on more than 2 lines but I'll always have reserved space on the right side of the cell to display another view.
So far, I tried doing this and it does not work. The UILabel
just runs right across the entire darned cell. I want it to be limited such that the text reaches maybe 3/4ths of the way through the cell before it wraps around onto a second line.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyle)UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
[cell.textLabel setLineBreakMode:NSLineBreakByWordWrapping];
cell.textLabel.minimumScaleFactor = 0;
[cell.textLabel setNumberOfLines:0];
[cell.textLabel setFont:[UIFont boldSystemFontOfSize:FONT_SIZE]];
[cell.textLabel setBackgroundColor:[UIColor clearColor]];;
NSString * labelText = myString; // lets just pretend this works. I have somethign else here.
CGSize constraint = CGSizeMake((CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2) - 100.0f), 20000.0f); // Ultimate cell
CGSize size = [labelText sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
[cell.textLabel setText:labelText];
[cell.textLabel setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2) - 180.0f, MAX(size.height, 36.0f))]; // Lets me get the height of the text, supposedly.
}
// more stuff....
}
Upvotes: 0
Views: 439
Reputation: 4901
Try this,you get height of label for string size
CGSize maximumLabelSize = CGSizeMake(width, FLT_MAX);
CGSize expectedLabelSize = [string sizeWithFont:font constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByTruncatingTail];
//you get height from expectedLabelSize.height
Upvotes: 0
Reputation: 3475
You need to subclass the UITableViewCell and set the size of the label in layoutSubviews
.
Although when you are creating the cell the label is being sized correctly, every time the cell is drawn on the screen your sizes are 'overwritten' by the UITableViewCell methods and so you don't see any change.
Upvotes: 2