放入粗体文本时出错 - Objective-C
我试图在文本的一部分上加“粗体”,但我做不到。
我正在使用这个:
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”;
我该怎么做呢?
I'm trying to put "bold" on a part of a text, but I can't do it.
I'm using this:
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];
I get an error "use of undeclared identifier NSFontAttributeName";
How can I do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从 iOS 6.0 开始,
NSFontAttributeName
应该可以正常工作。有关文档,请检查 NSAttributedString UIKit Additions。 https://developer.apple.com/library/ios/documentation/ UIKit/参考/NSAttributedString_UIKit_Additions/As of iOS 6.0,
NSFontAttributeName
should work fine. For docs check NSAttributedString UIKit Additions. https://developer.apple.com/library/ios/documentation/UIKit/Reference/NSAttributedString_UIKit_Additions/NSFontAttributeName
是 Mac 上 AppKit 中定义的常量,iOS 上不存在。事实上,UIKit 不提供用于样式化和绘制NSAttributedString
实例的附加功能。我发现有一篇博客文章显示如何使用底层核心文本属性。NSFontAttributeName
is a constant defined in AppKit on the Mac, it doesn't exist on iOS. In fact, UIKit doesn't provide additions for styling and drawingNSAttributedString
instances. There's a blog post I found that shows how to use the underlying Core Text attributes.