【问题标题】:How to convert uitextview content to a image in custom size?如何将 uitextview 内容转换为自定义大小的图像?
【发布时间】:2018-01-12 09:21:28
【问题描述】:

我创建了一个 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(iPhone 6),如果我写的内容少,比如1234,图片的宽度是40,那么如何将内容转换为自定义大小的图片,比如宽度是750,高度和内容高度一样长?有任何想法吗?非常感谢。

【问题讨论】:

    标签: objective-c uitextview nsattributedstring nstextattachment


    【解决方案1】:

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

    【讨论】:

    • 谢谢。把UIImage *image = [self imageFromTextView:self.tvContent width:750.0];改成UIImage *image = [self imageFromTextView:self.tvContent width:375.0];,效果很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-10
    相关资源
    最近更新 更多