3

我试图在文本的一部分上加上“粗体”,但我做不到。

我正在使用这个:

  NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"this is a part of the text"];
  NSRange selectedRange = NSMakeRange(5, 10); 

  [string beginEditing];

  [string addAttribute:NSFontAttributeName
          value:[NSFont fontWithName:@"Helvetica-Bold" size:12.0]
          range:selectedRange];
  [string endEditing];

我收到错误“使用未声明的标识符 NSFontAttributeName”;

我该怎么做?

4

2 回答 2

3

从 iOS 6.0 开始,NSFontAttributeName应该可以正常工作。对于文档,请检查 NSAttributedString UIKit Additions。https://developer.apple.com/library/ios/documentation/UIKit/Reference/NSAttributedString_UIKit_Additions/

于 2012-11-08T00:02:24.030 回答
2

NSFontAttributeName是 Mac 上 AppKit 中定义的常量,iOS 上不存在。事实上,UIKit 没有为样式和绘图NSAttributedString实例提供附加功能。我发现有一篇博文展示了如何使用底层核心文本属性

于 2012-02-08T09:45:58.787 回答