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