attributed text not working on TableView Cell

5.1k views Asked by At

trying to use a different font and font size on TabelViewCell but its not working.

Here is what I tried:

cell.textLabel.attributedText = [infoCells objectAtIndex:indexPath.row];

This code makes the app crash

Then i tried this:

cell.textLabel.text = [infoCells objectAtIndex:indexPath.row];

Now everything works but the font and the font size does not change.

Here are some more code from my app:

- (void)viewDidLoad
{
    [super viewDidLoad];

    infoCells  = [[NSMutableArray alloc] initWithCapacity:2];


    [infoCells addObject:@"DOI"];
    [infoCells addObject:@"WIS?"];
    [infoCells addObject:@"اللَّهُمَّ إِنِّي أَسْتَخِيرُكَ بِعِلْمِكَ وَأَسْتَقْدِرُكَ بِقُدْرَتِكَ"];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];


    // Configure the cell...

    cell.textLabel.attributedText = [infoCells objectAtIndex:indexPath.row];

    cell.textLabel.font = [UIFont fontWithName:@"Adobe Arabic" size:20];

    return cell;  

}
1

There are 1 answers

6
Dharmbir Singh On BEST ANSWER

You need to make a NSMutableAttributedString string and then assign it. So for more info see in this example.

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];


        // Configure the cell...

    NSMutableAttributedString *str=[[NSMutableAttributedString alloc] initWithString:[infoCells objectAtIndex:indexPath.row]];
 [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica-Bold" size:50] range:NSMakeRange(0, str.length)];

        cell.textLabel.attributedText = str;

        //cell.textLabel.font = [UIFont fontWithName:@"Adobe Arabic" size:20];

        return cell;  

    }