Reputation: 4685
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.
Any ideas why this is happening?
Upvotes: 0
Views: 1947
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
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
Reputation: 1161
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