How to convert text to Image on iOS? How to convert text to Image on iOS? ios ios

How to convert text to Image on iOS?


Several approaches are possible.

  1. If you have an existing UITextField, UITextView or UILabel that you just want to render as an image, you can employ the traditional snapshot approaches, such as:

    - (UIImage *)imageForView:(UIView *)view{    UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0);    if ([view respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)])        [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];  // if we have efficient iOS 7 method, use it ...    else        [view.layer renderInContext:UIGraphicsGetCurrentContext()];         // ... otherwise, fall back to tried and true methods    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext();    return image;}
  2. If you wanted a generic "create image from text" routine, in iOS 7, it would look like:

    - (UIImage *)imageFromString:(NSString *)string attributes:(NSDictionary *)attributes size:(CGSize)size{    UIGraphicsBeginImageContextWithOptions(size, NO, 0);    [string drawInRect:CGRectMake(0, 0, size.width, size.height) withAttributes:attributes];    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext();    return image;}

    The above will create an image whose size will vary based upon the text. Clearly, if you just want a fixed size image, then use constants frame, rather than dynamically building it.

    Anyway, you could then use the above like so:

    NSString *string = @"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";NSDictionary *attributes = @{NSFontAttributeName            : [UIFont systemFontOfSize:20],                             NSForegroundColorAttributeName : [UIColor blueColor],                             NSBackgroundColorAttributeName : [UIColor clearColor]};UIImage *image = [self imageFromString:string attributes:attributes size:self.imageView.bounds.size];
  3. If you need to support earlier iOS versions, you could use this technique:

    - (UIImage *)imageFromString:(NSString *)string font:(UIFont *)font size:(CGSize)size{    UIGraphicsBeginImageContextWithOptions(size, NO, 0);    [string drawInRect:CGRectMake(0, 0, size.width, size.height) withFont:font lineBreakMode: NSLineBreakByWordWrapping];    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext();    return image;}

There are many, many permutations of each of these. It just depends upon what you are trying to achieve.


Another approach is to simply have both UIImageView and UILabel/UITextView objects in the view, and if you have an image from the server, set the image of the UIImageView, and text, set the text of the UILabel/UITextView.


NSString *string = @"Some text";UIGraphicsBeginImageContext(CGSizeMake(80, 50));[string drawAtPoint:CGPointMake(10, 20)           withFont:[UIFont systemFontOfSize:12]];UIImage *result = UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();

You can start with this


Swift answer

If you only need to get a an image of the visible content of a UIFieldView, UITextView, or UILabel then you can use the following method.

func imageFromView(myView: UIView) -> UIImage {    UIGraphicsBeginImageContextWithOptions(myView.bounds.size, false, UIScreen.mainScreen().scale)    myView.drawViewHierarchyInRect(myView.bounds, afterScreenUpdates: true)    let image = UIGraphicsGetImageFromCurrentImageContext()    UIGraphicsEndImageContext()        return image}

Special case

When the text content scrolls out of the frame, such as in a UITextView, the solution above will only capture the visible area. In order to get everything, one solution is to resize a copy of the text view to the size of its content view before making an image of it.

func imageFromTextView(textView: UITextView) -> UIImage {    // Make a copy of the textView first so that it can be resized     // without affecting the original.    let textViewCopy = UITextView(frame: textView.frame)    textViewCopy.attributedText = textView.attributedText        // resize if the contentView is larger than the frame    if textViewCopy.contentSize.height > textViewCopy.frame.height {        textViewCopy.frame = CGRect(origin: CGPointZero, size: textViewCopy.contentSize)    }        // draw the text view to an image    UIGraphicsBeginImageContextWithOptions(textViewCopy.bounds.size, false, UIScreen.mainScreen().scale)    textViewCopy.drawViewHierarchyInRect(textViewCopy.bounds, afterScreenUpdates: true)    let image = UIGraphicsGetImageFromCurrentImageContext()    UIGraphicsEndImageContext()        return image}

Notes

  • One could also just resize the original text view and then resize it back again. However, it seemed simpler to just make a temporary copy.
  • Another way to get a copy of a view is to archive and unarchive it.
  • Or you could just write directly to a UIImage.


matomo