简体   繁体   中英

Error to put in bold text - Objective-C

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?

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 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 drawing NSAttributedString instances. There's a blog post I found that shows how to use the underlying Core Text attributes .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM