user906357
user906357

Reputation: 4685

Bold fonts in UITableView cell

I am trying to get the both the textLabel and the detailTextLabel fonts on my UITableViewCell to be bold. I easy got the textLabel to be bold like this:

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];

    NSString *titleString = [managedObject valueForKey:@"title"];
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:titleString];
    [string addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:kFontSize] range:NSMakeRange(0, string.length)];
    cell.textLabel.attributedText = string;

    cell.detailTextLabel.text = [managedObject valueForKey:@"subtitle"];
    cell.detailTextLabel.textColor = [UIColor grayColor];
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}

However when I try to make the detailTextLabel bold like this:

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];

    NSString *titleString = [managedObject valueForKey:@"title"];
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:titleString];
    [string addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:kFontSize] range:NSMakeRange(0, string.length)];
    cell.textLabel.attributedText = string;

    NSString *subtitleString = [managedObject valueForKey:@"subtitle"];
    NSMutableAttributedString *string2 = [[NSMutableAttributedString alloc]initWithString:subtitleString];
    [string2 addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:kSubFontSize] range:NSMakeRange(0, string2.length)];
    cell.detailTextLabel.attributedText = string2;
    cell.detailTextLabel.textColor = [UIColor grayColor];
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}

This crashes the app with a SIGABRT error.

enter image description here

enter image description here

Any ideas why this is happening?

Upvotes: 0

Views: 1947

Answers (3)

Webster Wu
Webster Wu

Reputation: 21

I have try the same code in my project, but it work well. I think you can try to check 'titleString' and 'subtitleString'. if the two variant is nil in some cell, the code will crash in NSMutableAttributedString init message.

Upvotes: 0

tiritea
tiritea

Reputation: 1279

To change the text to bold, keeping everything else the same (font, color, ...):

cell.textLabel.font = [UIFont fontWithDescriptor:[cell.textLabel.font.fontDescriptor fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold]
                                            size:cell.textLabel.font.pointSize];

ditto for cell.detailTextLabel.font

Upvotes: 1

raja
raja

Reputation: 1161

  • Pertaining to your question in title "Bold fonts in UITableView cell"
  • To bold the Table Cell texts, you can achieve in a easy way like below.

    [cell.textLabel setFont:[UIFont fontWithName:...];
    [cell.detailTextLabel setFont:[UIFont fontWithName:...];
    

Upvotes: 2

Related Questions