0

I have an UIImage that I want write a string onto. Currently I use the following code to place a string onto the image:

+ (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;
}

How can I extend this code to allow dragging the text across the image? How can I rotate the text with a UIRotationGestureRecognizer? How can I resize the font with UIPinchGestureRecognizer?

I am considering rendering the text as an image and dragging, rotating, and pinching the image. The problem I have with this is I'm afraid it will cause pixilation.

Tyler Hilbert
  • 2,107
  • 7
  • 35
  • 55
  • 4
    A simplest solution would be having a `UIImage` and `UILabel` added into an `UIView`, attach `UIPinchGesture` on `UILabel` to do what ever you want, when you finish, [capture](http://stackoverflow.com/a/3495396/790842) the `UIView` to create image, which will contain both the image and modified UILabel. – iphonic Jul 08 '16 at 19:01
  • @iphonic A UILabel is read only, how would I allow for user input using it? – Tyler Hilbert Jul 08 '16 at 19:21
  • http://stackoverflow.com/questions/9751307/iphone-ios-how-to-make-uirotationgesturerecognizer-and-uipinchgesturerecognizer This thread should help you. Dont write the image every time user moves the label, do it only when user hits save. – Bhumit Mehta Jul 08 '16 at 19:32
  • @TylerHilbert you can enable a textfield on tap gesture of uilabel and save the value. – iphonic Jul 09 '16 at 03:06
  • when i challenging similar situation this article help me a lot https://www.raywenderlich.com/6567/uigesturerecognizer-tutorial-in-ios-5-pinches-pans-and-more – Ali Kıran Jul 09 '16 at 19:55

0 Answers0