如何在ios tableview中减小字体大小并截断textlabel

时间:2012-02-06 06:37:02

标签: objective-c ios

我有一个表视图。我希望​​减少字体大小,并在cell.detailTextLabel.text中将字符串的长度截断为5个字符.cud你们帮帮我..下面是代码。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath 
{
    static NSString *CellIdentifier = @"Cell";
    //NSInteger SectionRows=[tableView1 numberOfRowsInSection:indexPath.section];
    //NSInteger Row=indexPath.row;

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease]; 
    }
    //cell.imageView.backgroundColor=[UIColor clearColor];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell.textLabel.text=[listOfItems objectAtIndex:indexPath.row];
    cell.textLabel.backgroundColor=[UIColor clearColor];
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;

    if (indexPath.row == 3) 
    {
        cell.detailTextLabel.text=@"Aktiviteter";
    }
    if (indexPath.row == 4)
    {
        cell.textLabel.textColor=[UIColor purpleColor];
        cell.detailTextLabel.text=@"Läs mer om Allegra";
    }
    if (indexPath.row == 5)
    {
        starImage.image = [UIImage imageNamed:@"add.png"];
        [cell.contentView addSubview:starImage];
    }

    return cell;
}

1 个答案:

答案 0 :(得分:2)

您可以通过设置小字体来减小detailTextLabel的字体大小。您也可以设置不同大小的不同字体。

cell.detailTextLabel.font = [UIFont systemFontOfSize:([UIFont systemFontSize]-2)];

仅显示字符串的前5个字符,请执行以下操作:

NSString *text = @"Aktiviteter";
text = [text substringToIndex:5];
cell.detailTextLabel.text = text;

所以,它看起来像

if (indexPath.row == 3) {
    cell.detailTextLabel.font = [UIFont systemFontOfSize:([UIFont systemFontSize]-2)];
    NSString *text = @"Aktiviteter";
    text = [text substringToIndex:5];
    cell.detailTextLabel.text = text;
}