How to draw text on an image from a UITextView consists of several lines of string?

I want to get a string with several lines from a UITextView and draw it on the image, the position of the text in the image should be exactly the same as the position in the UITextView, but I do not want jus to add the textView to the UIImageView, I need the new image to consist from this text, is this possible? any suggestion to do this?

+3
source share
2 answers

You can capture the contents of any UIView in a UIImage. First, create an empty UIView and put the UIImageView and UITextView as subviews:

// Assumes you have UIImageView *myImageView and UITextField *myTextField
UIView *parentView = [[UIView alloc] initWithFrame:CGRectZero];
[parentView addSubview:myImageView];
[myImageView setFrame:CGRectMake(0, 0, 100, 100)];
[parentView addSubview:myTextField];
[myTextField setFrame:CGRectMake(20, 20, 80, 20)];
[parentView sizeToFit];

Now create a graphical context and insert parentView into it:

UIGraphicsBeginImageContext([parentView bounds].size);
[[parentView layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

UIImage UIImageView UITextField , .

+6

, .

,

#import <QuartzCore/QuartzCore.h>

, .

+1

Source: https://habr.com/ru/post/1717257/


All Articles