stephensam
stephensam

Reputation: 91

How to reduce the font size and to truncate textlabel in ios tableview

i ve got a table view.i want to reduce the font size and truncate the length of the string to 5 characters in cell.detailTextLabel.text .cud u guys help me out..below is the code.

- (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;
}

Upvotes: 1

Views: 1142

Answers (1)

Ilanchezhian
Ilanchezhian

Reputation: 17478

You can reduce the font size of the detailTextLabel by setting the small font. You can set some different fonts too with different size.

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

Only to show first 5 characters of a string, do as follows:

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

So, it would be look like

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

Upvotes: 2

Related Questions