我有一个想要写一个字符串的UIImage。目前,我使用以下代码将字符串放在图像上:
+ (UIImage*)writeText:(UIImage*) photo :(CGPoint)point {
NSString *text = @"Hello World!";
UIFont *font = [UIFont fontWithName:@"HelveticaNeue" size:21];
UIGraphicsBeginImageContext(photo.size);
[photo drawInRect:CGRectMake(0,0,photo.size.width,photo.size.height)];
CGRect rect = CGRectMake(point.x, (point.y - 5), photo.size.width, photo.size.height);
[[UIColor whiteColor] set];
NSMutableAttributedString* attString = [[NSMutableAttributedString alloc] initWithString:text];
NSRange range = NSMakeRange(0, [attString length]);
[attString addAttribute:NSFontAttributeName value:font range:range];
[attString addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:range];
NSShadow* shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor darkGrayColor];
shadow.shadowOffset = CGSizeMake(1.0f, 1.5f);
[attString addAttribute:NSShadowAttributeName value:shadow range:range];
[attString drawInRect:CGRectIntegral(rect)];
UIImage* newPhoto = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newPhoto;
}
如何扩展此代码以允许在图像上拖动文本?如何使用UIRotationGestureRecognizer旋转文本?如何使用UIPinchGestureRecognizer调整字体大小?
我正在考虑将文字渲染为图像并拖动,旋转和捏合图像。我遇到的问题是我担心会导致像素化。