我正在尝试让我的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错误。


你知道为什么会发生这种情况吗?
发布于 2014-07-23 11:31:49
[cell.textLabel setFont:UIFont fontWithName:...;[cell.detailTextLabel setFont:UIFont UIFont
发布于 2016-03-22 08:37:18
要将文本更改为粗体,并保持其他内容不变(字体、颜色等):
cell.textLabel.font = [UIFont fontWithDescriptor:[cell.textLabel.font.fontDescriptor fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold]
size:cell.textLabel.font.pointSize];cell.detailTextLabel.font也是如此
发布于 2016-03-22 10:59:41
我已经在我的项目中尝试了相同的代码,但它工作得很好。我想你可以试着检查'titleString‘和'subtitleString’。如果这两个变量在某些单元格中为空,代码将在NSMutableAttributedString初始化消息中崩溃。
https://stackoverflow.com/questions/24900796
复制相似问题