0

I have a UITextView that takes up an entire scene (view). The textviews length varies depending on how much content there is. It could be very long and therefore does not all appear on the screen -- you have to scroll to see the rest. Also, the content could have images inside it using NSTextAttachment. My goal is to create an image of the entire textview. It should even include content that is offscreen (not currently visible). Here is what I have so far:

func textViewImage() -> UIImage {

    UIGraphicsBeginImageContext(textView.contentSize)

    let savedContentOffset: CGPoint = textView.contentOffset
    let savedFrame: CGRect = textView.frame

    textView.contentOffset = CGPointZero
    textView.frame = CGRectMake(0, 0, textView.contentSize.width, textView.contentSize.height)

    textView.layer.renderInContext(UIGraphicsGetCurrentContext()!)
    let image = UIGraphicsGetImageFromCurrentImageContext()

    textView.contentOffset = savedContentOffset
    textView.frame = savedFrame

    UIGraphicsEndImageContext()

    return image
}

This does not work. It only takes a picture of what is currently visible on the screen. I check to see if this code is working or not working by trying to share it by text message or mail:

 @IBAction func share(sender: UIBarButtonItem) {

    let shareTextViewImage = textViewImage()
    let activityController = UIActivityViewController(activityItems: [shareTextViewImage], applicationActivities: nil)

    self.presentViewController(activityController, animated: true, completion: nil)

}

I got as far as I did thanks to this post on SO and others Getting a screenshot of a UIScrollView, including offscreen parts.

Look forward to some help! Thanks!

Update 1: I created this variable var pageSize: CGSize = CGSizeMake(320, 800) and tried adding it like so: UIGraphicsBeginImageContext(pageSize). This does not work either since it only takes the current screen shot. Anyways this is a fixed size which wouldn't work in the long run since the content size could change --- just wanted to see how this would work out.

Still searching for answers...

Community
  • 1
  • 1
JEL
  • 1,540
  • 4
  • 23
  • 51
  • Instead of rendering the text view, why not just draw the text view's text into the graphics context. You just need the text and the proper frame for it (which you have). – rmaddy Sep 22 '15 at 03:41
  • @rmaddy Could you show me how to do this in code? Sorry Im a new to this and don't understand how this code works well. – JEL Sep 22 '15 at 16:25
  • @rmaddy I attempted what you mentioned and updated my question. It did not work. I'm not even sure if I did exactly as you said to do. Am I on the right track? – JEL Sep 22 '15 at 21:57

1 Answers1

3

I figured it out myself. This works for me:

    func textViewImage() -> UIImage {

    var image: UIImage? = nil

    UIGraphicsBeginImageContextWithOptions(textView.contentSize, textView.opaque, 0.0)

    let savedContentOffset: CGPoint = textView.contentOffset
    let savedFrame: CGRect = textView.frame

    textView.contentOffset = CGPointZero
    textView.frame = CGRectMake(0, 0, textView.contentSize.width, textView.contentSize.height)

    textView.layer.renderInContext(UIGraphicsGetCurrentContext()!)
    image = UIGraphicsGetImageFromCurrentImageContext()

    textView.contentOffset = savedContentOffset
    textView.frame = savedFrame

    UIGraphicsEndImageContext()

    return image!
}

And if you want to share this image:

    @IBAction func share(sender: UIBarButtonItem) {

    let img = textViewImage()
    let shareItem = [img]

    let activityController = UIActivityViewController(activityItems: shareItem, applicationActivities: nil)
    self.presentViewController(activityController, animated: true, completion: nil)

}
JEL
  • 1,540
  • 4
  • 23
  • 51
  • Simple and straight answer! But, what if I'd want to modify the content in UITextView? Ex: change the font size, add a background colour, etc., – Shyam May 24 '17 at 09:58
  • Also, I have a frame around my `UITextView` to replicate a `UITextField`, and even that is included as the image is drawn!! :-/ – Shyam May 25 '17 at 06:40
  • @Shyam Hm good questions. I haven't worked on this for a while now so not entirely sure. From what I remember, it will take a picture of whatever is currently visible on the screen. I'm not sure if you could change the font, size, etc before taking the picture all at the same time. For my simple use case, I simply took a picture of 100% of what was on screen as is. – JEL May 25 '17 at 16:50
  • :) ... I don't need to change anything, and your code works like a charm. I have a border around my `UITextView`, and even that appears in the image, but I would not want that to happen. I'm very new to swift, piecing everything together, as I learn. But, would you explain your code? Thanks in advance... – Shyam Jun 09 '17 at 03:56
  • i want to take screenshot of all the content of the textView even if scrolled/off screen but it just gave me screenshot of the view that is equal to textview frame only, not the whole content. @JEL – MALIKK HABIB UR REHMAN Jan 26 '22 at 11:11