我试图让我的UITableViewCell上的textLabel和detailTextLabel字体都变为粗体。我很容易将textLabel变成这样的大胆:
- (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];
}
然而,当我尝试使detailTextLabel像这样加粗:
- (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];
}
这会导致应用程序出现SIGABRT错误。
为什么会发生这种情况?
答案 0 :(得分:2)
要加粗表格单元格文本,您可以通过以下方式轻松实现。
[cell.textLabel setFont:[UIFont fontWithName:...];
[cell.detailTextLabel setFont:[UIFont fontWithName:...];
答案 1 :(得分:1)
要将文本更改为粗体,请保持其他所有内容(字体,颜色,...):
cell.textLabel.font = [UIFont fontWithDescriptor:[cell.textLabel.font.fontDescriptor fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold]
size:cell.textLabel.font.pointSize];
同样适用于cell.detailTextLabel.font
答案 2 :(得分:0)
我在我的项目中尝试了相同的代码,但它运行良好。 我想你可以试着检查一下titleString'和' subtitleString'。 如果两个变量在某个单元格中为nil,则代码将在NSMutableAttributedString init消息中崩溃。