Draw Angled/Rotated MultiLine Text - Core Text + Core Graphics
Asked Answered
S

1

6

After going through several blogs & forums I didn't find an appropriate solution for drawing inclined/angled text using core Text on a views context.

So here is how it goes.

I have a view whose - (void)drawRect:(CGRect)rect is invoked to draw a string (multi or single line text) on screen.

CODE:

- (void)drawRect:(CGRect)rect
{
    NSString *text = @"This is some text being drawn by CoreText!\nAnd some more text on another line!";

    //Core Text (Create Attributed String)

    UIColor *textColor = [UIColor blackColor];
    CGColorRef color = textColor.CGColor;

    CTFontRef font = CTFontCreateWithName((CFStringRef) @"HelveticaNeue", 20.0, NULL);

    CTTextAlignment theAlignment = kCTTextAlignmentLeft;

    CFIndex theNumberOfSettings = 1;
    CTParagraphStyleSetting theSettings[1] =
    {
        { kCTParagraphStyleSpecifierAlignment, sizeof(CTTextAlignment),
            &theAlignment }
    };

    CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(theSettings, theNumberOfSettings);

    NSDictionary *attributesDict = [NSDictionary dictionaryWithObjectsAndKeys:
                                    CFBridgingRelease(font), (NSString *)kCTFontAttributeName,
                                    color, (NSString *)kCTForegroundColorAttributeName,
                                    paragraphStyle, (NSString *) kCTParagraphStyleAttributeName,
                                    nil];


    NSAttributedString *stringToDraw = [[NSAttributedString alloc] initWithString:text attributes:attributesDict];

    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)stringToDraw);

    //Create Frame
    CGMutablePathRef path = CGPathCreateMutable();

    CGAffineTransform transform = CGAffineTransformMakeScale(1, -1);
    //First translate your image View according to transform
    transform = CGAffineTransformTranslate(transform, 0, - self.bounds.size.height);
    // Then whenever you want any point according to UIKit related coordinates apply this transformation on the point or rect.
    CGRect frameText = CGRectMake(60, 100, 200, 200);
    CGRect newRectForUIKit = CGRectApplyAffineTransform(frameText, transform);
    CGPathAddRect(path, NULL, newRectForUIKit);

    CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);

    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSetTextMatrix(ctx, CGAffineTransformIdentity);
    CGContextTranslateCTM(ctx, 0, ([self bounds]).size.height );
    CGContextScaleCTM(ctx, 1.0, -1.0);

    //Draw Frame

    CTFrameDraw(frame, ctx);
    //Release all retained objects
    CFRelease(path);
}

Output:

enter image description here

Apart from drawing text I want to add an angle to the entire drawn text. Something like this(Required output)

In this particular image the drawn text is rotated by 90 degreesA

So how do I add an rotation angle to the drawn text in core text?

Note: 1)A single context can have multiple drawn text objects with their respective angles as shown below

enter image description here

I hope my question is clear.

Sattler answered 27/1, 2014 at 20:48 Comment(0)
S
9

Apply the rotation to the context before drawing the CTFrameRef into it.

Edit : If you want multiple angles, you need to save/restore the graphics states, each time. Something like :

- (void)drawRect:(CGRect)rect
{
    NSString *text = @"This is some text being drawn by CoreText!\nAnd some more text on another line!";

    //Core Text (Create Attributed String)

    UIColor *textColor = [UIColor blackColor];
    CGColorRef color = textColor.CGColor;

    CTFontRef font = CTFontCreateWithName((CFStringRef) @"HelveticaNeue", 20.0, NULL);

    CTTextAlignment theAlignment = kCTTextAlignmentLeft;

    CFIndex theNumberOfSettings = 1;
    CTParagraphStyleSetting theSettings[1] =
    {
        { kCTParagraphStyleSpecifierAlignment, sizeof(CTTextAlignment),
            &theAlignment }
    };

    CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(theSettings, theNumberOfSettings);

    NSDictionary *attributesDict = [NSDictionary dictionaryWithObjectsAndKeys:
                                    CFBridgingRelease(font), (NSString *)kCTFontAttributeName,
                                    color, (NSString *)kCTForegroundColorAttributeName,
                                    paragraphStyle, (NSString *) kCTParagraphStyleAttributeName,
                                    nil];


    NSAttributedString *stringToDraw = [[NSAttributedString alloc] initWithString:text attributes:attributesDict];

    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)stringToDraw);

    //Create Frame
    CGMutablePathRef path = CGPathCreateMutable();
    CGRect frameText = CGRectMake(60, 100, 200, 200);
    CGPathAddRect(path, NULL, frameText);

    CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    CGContextSaveGState(ctx); /* save Graphic State for context rotation */

    // transform (rotate) context
    CGAffineTransform transform = CGAffineTransformMakeTranslation(self.bounds.size.width / 2.f, self.bounds.size.height / 2.f);
    CGFloat rotation = -M_PI / 2.f;
    transform = CGAffineTransformRotate(transform,rotation);
    transform = CGAffineTransformTranslate(transform,-self.bounds.size.width / 2.f, -self.bounds.size.height / 2.f);
    CGContextConcatCTM(ctx, transform);

    CGContextSaveGState(ctx);

    CGContextSetTextMatrix(ctx, CGAffineTransformIdentity);
    CGContextTranslateCTM(ctx, 0, ([self bounds]).size.height );
    CGContextScaleCTM(ctx, 1.0, -1.0);

    //Draw Frame

    CTFrameDraw(frame, ctx);
    //Release all retained objects
    CFRelease(path);

    CGContextRestoreGState(ctx);

    CGContextRestoreGState(ctx); /* restore Graphic State for context rotation */

     CGContextSaveGState(ctx); /* save Graphic States for another drawing */

     /* lets draw another string with different angle */
     attributesDict = [NSDictionary dictionaryWithObjectsAndKeys:
                                CFBridgingRelease(CTFontCreateWithName((CFStringRef) @"HelveticaNeue", 14.0, NULL)), (NSString *)kCTFontAttributeName,
                                [UIColor yellowColor].CGColor, (NSString *)kCTForegroundColorAttributeName,
                                nil];


     stringToDraw = [[NSAttributedString alloc] initWithString:@"another piece of text to drawn on same context with no angle" attributes:attributesDict];

     framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)stringToDraw);

     path = CGPathCreateMutable();
     CGPathAddRect(path, NULL, CGRectMake(60, -100, 200, 200));

     frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);
     CFRelease(path);
     CGContextSetTextMatrix(ctx, CGAffineTransformIdentity);
     CGContextTranslateCTM(ctx, 0, ([self bounds]).size.height );
     CGContextScaleCTM(ctx, 1.0, -1.0);

     CTFrameDraw(frame, ctx);

     /**
      * @note don't forget to restore a previously saved GState, or this will be source of problems
      */
     CGContextRestoreGState(ctx);

     CFRelease(frame);
     CFRelease(framesetter);
}

enter image description here

Spurn answered 28/1, 2014 at 10:43 Comment(4)
Vote up for the Answer; But I had added a note at bottom of my question. Please see my edit (I added a new screen shot).Sattler
@Pranav : you need to save/restore graphics states, to do this. see my editSpurn
Thanks for well commented code. Good to know about Save & Restore Context CG Functions.Sattler
@Spurn do you think rotating the context to draw n numbers of text is the most optimized way of doing it?Cardin

© 2022 - 2024 — McMap. All rights reserved.