Display NSAttributedString using CoreText
Asked Answered
F

3

8

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!!

Fanjet answered 30/11, 2011 at 6:35 Comment(1)
If you don't want to use those wrappers, have a look inside, how they work.Flotage
P
10

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).

Prelature answered 11/3, 2012 at 17:28 Comment(2)
Does this render the line in arbitrary context created by user(using bitmap data), or the context must be current context?Dripping
@DeepakSharma This renders to a user created context (in this case, I just called it context).Prelature
C
13

Simplest way? Something like this:

CGContextRef context = UIGraphicsGetCurrentContext();

// Flip the coordinate system
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextTranslateCTM(context, 0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);

// Create a path to render text in
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, self.bounds );

// An attributed string containing the text to render
NSAttributedString* attString = [[NSAttributedString alloc]
                                  initWithString:...];

// create the framesetter and render text
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attString); 
CTFrameRef frame = CTFramesetterCreateFrame(framesetter,
                         CFRangeMake(0, [attString length]), path, NULL);

CTFrameDraw(frame, context);

// Clean up
CFRelease(frame);
CFRelease(path);
CFRelease(framesetter);
Chilpancingo answered 30/11, 2011 at 11:5 Comment(1)
what do you think about this? #55799543Hawken
P
10

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).

Prelature answered 11/3, 2012 at 17:28 Comment(2)
Does this render the line in arbitrary context created by user(using bitmap data), or the context must be current context?Dripping
@DeepakSharma This renders to a user created context (in this case, I just called it context).Prelature
T
0

Starting from ios 6 you can do the following :

    NSMutableParagraphStyle *paragrahStyle = [[NSMutableParagraphStyle alloc] init];
[paragrahStyle setLineSpacing:40];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragrahStyle range:NSMakeRange(0, [labelText length])];

cell.label.attributedText = attributedString ;
Theatrical answered 12/4, 2013 at 5:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.