Search code examples
iphoneioscore-textnsattributedstring

Display NSAttributedString using CoreText


I have heard that I can display a NSAttributedString using CoreText, can anyone say me how (The simplest way)?

Please, don't answer with CATextLayer or OHAttributedLabel.

I know that there are a lot of questions about this in this forum, but I haven't find the answer

Thanks!!


Solution

  • I think that the simplest way (using Core Text) is:

     // Create the CTLine with the attributed string
     CTLineRef line = CTLineCreateWithAttributedString(attrString); 
    
     // Set text position and draw the line into the graphics context called context
     CGContextSetTextPosition(context, x,  y);
     CTLineDraw(line, context);
    
     // Clean up
     CFRelease(line);
    

    Using a Framesetter is more efficient IF you are drawing lots of text but this is the method recommended by Apple if you just need to display a small amount of text (like a label) and doesn't require you to create a path or frame (since it is done for you automatically by CTLineDraw).