Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it possible to rotate the character using NSAttributedString

Using NSAttributedString we can change the colour of string etc. Is it possible to tilt a specific character in a string. E.g

enter image description here

answers will be appreciated.

I found a solution which is not unto the mark. If someone corrects the code that while be helpful

- (void)drawRect:(CGRect)rect {

    CTFontRef sysUIFont = CTFontCreateUIFontForLanguage(kCTFontSystemFontType,
                                                        14.0, NULL);


    // blue
    CGColorRef color = [UIColor blueColor].CGColor;

    // single underline
    //NSNumber *underline = [NSNumber numberWithInt:kCTUnderlineStyleSingle];

    NSDictionary *attributesDict = [NSDictionary dictionaryWithObjectsAndKeys:
                                    (__bridge id)sysUIFont, (id)kCTFontAttributeName,
                                    color, (id)kCTForegroundColorAttributeName, nil];

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetTextMatrix(context, CGAffineTransformIdentity);
    CGContextTranslateCTM(context, 0, self.bounds.size.height);

    CGContextScaleCTM(context, 1.0, -1.0);


    for (int i =0 ; i < 4; i++)
    {

        NSString *letter =[@"TEXT" substringWithRange:NSMakeRange(i, 1)];

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

       // CGSize letterSize =[letter sizeWithAttributes:@{NSFontAttributeName:self.font}];


        // draw
        CTLineRef line = CTLineCreateWithAttributedString(
                                                          (CFAttributedStringRef)stringToDraw);

        if(i==3){
            CGContextRotateCTM (context, radians(90));

             //18 & 17 are hard coded values
            CGContextSetTextPosition(context, (10.0 * i)- 18, 10.0 - (17*i));

        }
        else{
        CGContextSetTextPosition(context, 10.0 * i, 10.0);
        }

        CTLineDraw(line, context);

        // clean up
        CFRelease(line);

        stringToDraw = nil;

    }


    CFRelease(sysUIFont);
    //stringToDraw = nil;


}

OutPut :

enter image description here

like image 472
Anand Avatar asked Oct 31 '22 18:10

Anand


1 Answers

The NSAffineTransform class provides methods for creating, concatenating, and applying affine transformations.

rotateByDegrees: Applies a rotation factor (measured in degrees) to the receiver’s transformation matrix.

rotateByRadians: Applies a rotation factor (measured in radians) to the receiver’s transformation matrix.

Example:

- (void)drawRect:(NSRect)rect
{
    NSAttributedString * myString = [[NSAttributedString alloc] initWithPath:mypath documentAttributes:NULL];
    NSAffineTransform* xform = [NSAffineTransform transform];
    [myString drawAtPoint:NSMakePoint(10,10)];
    [xform rotateByDegrees:-90.0];
    [xform concat];
    [myString drawAtPoint:NSMakePoint(-150,0)];
    [xform invert];
    [xform concat];
    [myString drawAtPoint:NSMakePoint(15,15)];
    [myString release];
}
like image 186
Shamsudheen TK Avatar answered Nov 15 '22 05:11

Shamsudheen TK