Eugene Trapeznikov
Eugene Trapeznikov

Reputation: 3240

Let's make cell.textLabel shorter

I got an app with some tables.

In this table there are two labels - the textLabel with titles and the detailTextLabel with dates. But when title is really long, it is showed over the detailTextLabel.

How can I solve this problem?

P.S.
I tried to override layoutSubviews method in this way:

- (void)layoutSubviews {
    [super layoutSubviews];

    CGSize size = self.bounds.size;
    CGRect frame = CGRectMake(4.0f, 4.0f, size.width, size.height); 
    self.textLabel.frame =  frame;
    self.textLabel.contentMode = UIViewContentModeScaleAspectFit;
}

But it doesn't work

Upvotes: 0

Views: 1122

Answers (2)

Eugene Trapeznikov
Eugene Trapeznikov

Reputation: 3240

Firstly i create empty cell. Then add there UILabel as subView with tag. And change UILabel's frame

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Recent Dream";

    UITableViewCell *cell;// = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

        NSLog(@"new cell");

        UILabel *textLabel = [[UILabel alloc] init];
        textLabel.frame = CGRectMake(10, 0, self.view.frame.size.width -110, 48);
        textLabel.backgroundColor = [UIColor clearColor];
        textLabel.font = [UIFont boldSystemFontOfSize:16];
        [textLabel setTag:1];
        [cell.contentView addSubview:textLabel];
    }

    // Configure the cell...

    Dream *tempDream = [self.dreams objectAtIndex:indexPath.row];

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"MM/dd/yyyy"];

    //Optionally for time zone converstions
    [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"..."]];

    NSString *stringFromDate = [formatter stringFromDate:tempDream.dateAdd];

    UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"bodyWrapper.png"]];

    cell.contentView.backgroundColor = background;

    UILabel *textLabel = (UILabel *)[cell.contentView viewWithTag:1];

    textLabel.text = tempDream.title;

    cell.textLabel.text = @"";
    cell.detailTextLabel.text = stringFromDate;

    return cell;
}

Upvotes: 1

alexmorhun
alexmorhun

Reputation: 1983

float newWidth = 30; //to try increase the value, while all will be right.
CGRect frame = CGRectMake(4.0f, 4.0f, size.width - newWidth, size.height);

Upvotes: 0

Related Questions