如何将 uitextview 内容转换为自定义大小的图像?

How to convert uitextview content to a image in custom size?

我创建了一个 uitextview,我可以使用 NSAttributedString 和 NSTextAttachment 向其中添加文本和图像

  self.tvContent = [[UITextView alloc] initWithFrame:CGRectMake(0, 20, 
  self.view.frame.size.width, self.view.frame.size.height - 64)];
  self.tvContent.font = [UIFont fontWithName:@"Arial" size:16.0f];
  self.tvContent.backgroundColor = [UIColor whiteColor];
  self.tvContent.textContainerInset = UIEdgeInsetsMake(10, 10, 10, 10);
  self.tvContent.contentInset = UIEdgeInsetsMake(0, 0, 10, 0);
  self.tvContent.layoutManager.allowsNonContiguousLayout=NO;
  [self.view addSubview:self.tvContent];

然后我使用以下代码将其内容转换为图像

  - (UIImage *)imageFromView:(UITextView *)view {
    CGRect tmpFrame = self.view.frame;
    CGRect aFrame = view.frame;
    aFrame.size.height  = [view sizeThatFits:[[UIScreen mainScreen] bounds].size].height;
    view.frame = aFrame;
    UIGraphicsBeginImageContext([view sizeThatFits:[[UIScreen mainScreen] bounds].size]);
    CGContextRef resizedContext = UIGraphicsGetCurrentContext();
    [view.layer renderInContext:resizedContext];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    view.frame = tmpFrame;
    return image;
 }

这张图片的最大宽度是375(iPhone6),如果我写的内容比较少,比如1234,图片的宽度是40,那么如何把内容转成图片自定义尺寸,比如宽度是750,高度和内容高度一样长?有任何想法吗?非常感谢。

UITextView 仅在其当前帧内呈现,因此您需要在呈现前将帧调整为所需大小,或者创建一个临时 UITextView 对象以用于呈现目的。

以下是使用临时对象进行渲染的方法:

// Your call to get the image
    UIImage *image = [self imageFromTextView:self.tvContent width:750.0]; // Any width you want

// Your text view properties that also can be used for rendering purpose
- (void)setupTextViewProperties:(UITextView *)textView {
    textView.font = [UIFont fontWithName:@"Arial" size:16.0f];
    textView.backgroundColor = [UIColor whiteColor];
    textView.textContainerInset = UIEdgeInsetsMake(10, 10, 10, 10);
    textView.contentInset = UIEdgeInsetsMake(0, 0, 10, 0);
    textView.layoutManager.allowsNonContiguousLayout=NO;
}

设置用于呈现的临时 UITextView 对象。

- (UIImage *)imageFromTextView:(UITextView *)view width:(CGFloat)desiredWidth {
    // Create a new temporary UITextView to use for rendering
    CGRect frame = CGRectMake(0.0, 0.0, desiredWidth, 0.0);
    UITextView *temporaryTextView = [[UITextView alloc] initWithFrame:frame];

    // Setup the temporary text view
    // You can have different font, colors etc from the original view
    [self setupTextViewProperties:temporaryTextView];
    temporaryTextView.attributedText = view.attributedText;

    // Calculate the height needed for content with width
    CGSize size = CGSizeMake(desiredWidth, 0.0);
    size = [temporaryTextView sizeThatFits:size];

    // Resize the temporary view
    // Note that the calculated size can contain a width smaller that desired with
    temporaryTextView.frame = CGRectMake(0.0, 0.0, desiredWidth, size.height);

    // Get the image
    return [self imageFromView:temporaryTextView];
}

可以将任何视图渲染为图像的助手。

- (UIImage *)imageFromView:(UIView *)view {
    CGSize size = view.bounds.size;
    UIGraphicsBeginImageContextWithOptions(size, YES, 0.0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [view.layer renderInContext:context];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}