1

In my app and using Objective-C I need to implement the following functionality if possible. I would like to know if it is possible to generate a screenshot of the selected text from a UITextView. I know it sounds like a weird question but it is an important and a required functionality.

I know how to just create a screenshot programmatically but would like the region of the screenshot to be selectable based on the selected text, I hope you know what I mean.

Thank you.

Wael
  • 489
  • 6
  • 19
  • Suggestion : get selected step -> Write text in UIImage -> save that canvas. Your work is done, http://stackoverflow.com/questions/11298266/write-text-on-image-in-objective-c-iphone-font-size-of-simulator-different-from – Jageen Dec 15 '14 at 10:38

2 Answers2

1

This code will help you:

NSString *selectedText = [textView textInRange:textView.selectedTextRange];
NSDictionary *textOptions = @{NSFontAttributeName : textView.font, NSForegroundColorAttributeName : textView.textColor};
CGSize textSize = [selectedText sizeWithAttributes:textOptions];

UIGraphicsBeginImageContextWithOptions(textSize, NO, 0);
[selectedText drawInRect:CGRectMake(0, 0, textSize.width, textSize.height) withAttributes:textOptions];
UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Vitalii Gozhenko
  • 9,220
  • 2
  • 48
  • 66
  • Thats just perfect, thank you so much. This is exactly what I was hoping for. I really appreciate it. – Wael Dec 16 '14 at 09:04
0

I'm going to supposed your UITextView is self.textView, using quartz you can do:

UIGraphicsBeginImageContextWithOptions(self.textView.bounds.size, NO, 0.0);

[self.textView.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *newImageWithMyTextView = UIGraphicsGetImageFromCurrentImageContext();

More info search Core Graphics in Apple docs

Maveňツ
  • 1
  • 12
  • 50
  • 89
Onik IV
  • 5,007
  • 2
  • 18
  • 23