首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UITableView单元格中的粗体

问UITableView单元格中的粗体
EN

Stack Overflow用户
提问于 2014-07-23 10:17:34
回答 3查看 1.8K关注 0票数 0

我正在尝试让我的UITableViewCell上的textLabel和detailTextLabel字体都加粗。我很容易就让textLabel变得像这样大胆:

代码语言:javascript
复制
- (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变得粗体时:

代码语言:javascript
复制
- (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错误。

​

​

​

​

你知道为什么会发生这种情况吗?

EN

回答 3

Stack Overflow用户

发布于 2014-07-23 11:31:49

  • 关于你在标题“UITableView单元格中加粗字体”的问题
  • 要加粗表格单元格文本,你可以用一种简单的方式实现,如下图所示。

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

票数 2
EN

Stack Overflow用户

发布于 2016-03-22 08:37:18

要将文本更改为粗体,并保持其他内容不变(字体、颜色等):

代码语言:javascript
复制
cell.textLabel.font = [UIFont fontWithDescriptor:[cell.textLabel.font.fontDescriptor fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold]
                                            size:cell.textLabel.font.pointSize];

cell.detailTextLabel.font也是如此

票数 1
EN

Stack Overflow用户

发布于 2016-03-22 10:59:41

我已经在我的项目中尝试了相同的代码,但它工作得很好。我想你可以试着检查'titleString‘和'subtitleString’。如果这两个变量在某些单元格中为空,代码将在NSMutableAttributedString初始化消息中崩溃。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24900796

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档