0

I need to rotate the String and save this rotating string as UIImage.

Let say my given String is = HELLO WORLD

Now i need to first rotate(say 45 degree) it and than saved as UIImage so i can show this UIImage into my UIImageView

Please help me . So i can rotate the String and save this rotating string as UIImage in Swift?.

I found this answer Drawing rotated text with NSString drawInRect. But not able to achieve the result.

Community
  • 1
  • 1
Rizwan Shaikh
  • 2,824
  • 2
  • 27
  • 49

1 Answers1

0

May be the below code helps u,I achieved the same result of rotating the string in 45 degree

-(UIImage*) drawText:(NSString*) text
         atPoint:(CGPoint)   point
{
UIGraphicsBeginImageContextWithOptions(CGSizeMake(65, 65),NO,0.0);

CGRect rect = CGRectMake(point.x, point.y, 50, 30);
[[UIColor whiteColor] set];
CGContextConcatCTM(UIGraphicsGetCurrentContext(), CGAffineTransformMakeTranslation(32.5, 32.5));
CGContextConcatCTM(UIGraphicsGetCurrentContext(), CGAffineTransformMakeRotation(M_PI / 4));
CGContextConcatCTM(UIGraphicsGetCurrentContext(), CGAffineTransformMakeTranslation(-32.5, -32.5));
//[text drawInRect:CGRectIntegral(rect) withFont:font ];

NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
[paragraphStyle setLineBreakMode:NSLineBreakByWordWrapping];
[paragraphStyle setAlignment:NSTextAlignmentCenter];
NSDictionary *attributes = @{ NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-CondensedBold" size:12],
                              NSForegroundColorAttributeName: [UIColor blackColor],

                              NSParagraphStyleAttributeName:paragraphStyle};

[text drawInRect:CGRectIntegral(rect) withAttributes:attributes];


UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return newImage;
}

Below is screenshot where i achieved the resultenter image description here

Mukesh
  • 3,680
  • 1
  • 15
  • 32
  • can u help me to find out the height and width of string as my string content is dynamic – Rizwan Shaikh Jun 29 '15 at 06:12
  • you can check in stackoverflow.like the above link http://stackoverflow.com/questions/1324379/how-to-calculate-the-width-of-a-text-string-of-a-specific-font-and-font-size – Mukesh Jun 29 '15 at 06:18