将 attributeText 分配给 UILabel 会导致应用程序崩溃,并显示“NSConcreteMutableAttributedString initWithString:: nil value”

Mar*_*llo 4 objective-c uitableview nsattributedstring ios

这是我的 tableViewCell 代码的实现,我在这里所做的只是从 MainViewController 获取数据并将其分配给两个不同的 UILabel。

问题出在 UILabel *subTitle 中,当我使用 attributeText 分配它以传递所需的行距时,应用程序在滚动 tableView 后崩溃。但如果我简单地用 .text = 分配它,它就可以正常运行,不会崩溃。

@property (strong, nonatomic) IBOutlet UILabel *title;
@property (strong, nonatomic) IBOutlet UILabel *subTitle;

- (void)configureWithPosts:(Data *)data {

  self.title.text = data.title;

  NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:data.excerpt];
  NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
  [attributeString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [attributeString length])];
  [paragraphStyle setLineSpacing:3];

  self.subTitle.textColor = [UIColor colorWithRed:102.0f/255.0f green:102.0f/255.0f blue:102.0f/255.0f alpha:1.0];
  self.subTitle.attributedText = attributeString;

}
Run Code Online (Sandbox Code Playgroud)

谁能告诉我我在这里做错了什么?

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'NSConcreteMutableAttributedString initWithString:: nil value'
Run Code Online (Sandbox Code Playgroud)

小智 5

我在不小心尝试将字体设置为不存在的字体时遇到了此错误:

NSMutableAttributedString *selectedString = [[NSMutableAttributedString alloc] initWithString:self.allUsersButton.titleLabel.text];
[selectedString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:1] range:NSMakeRange(0, selectedString.length)];
[selectedString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica-Neue-Bold" size:16.0f]
Run Code Online (Sandbox Code Playgroud)

这里的问题是 NSFontAttributeName 应该是fontWithName:@"Helvetica-Neue-Bold"