Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

attributed text not working on TableView Cell

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;  

}
like image 964
user1951876 Avatar asked Mar 22 '23 10:03

user1951876


1 Answers

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;  

    }
like image 138
Dharmbir Singh Avatar answered Apr 05 '23 23:04

Dharmbir Singh