I'm creating an image to export in my iOS app. I am using UIGraphicsBeginImageContext to draw the elements of the image but I've been struggling drawing a UITextView in the image.
First I create a parent view for rendering:
var parentView : UIView = UIView(frame: CGRectMake(0, 0, 700, 700))
Then I programatically create the textView and add all the properties. I give it x=100 and y=100 to align the text in the image. Width = 500 is good to have a margin of 100px on each side:
var textView: UITextView = UITextView(frame: CGRectMake(100, 100, 500, 350)) textView.text = myText textView.textContainerInset = UIEdgeInsetsMake(20, 20, 20, 20) textView.backgroundColor = UIColor(red: 255, green: 255, blue: 255, alpha: 0.70) textView.font = UIFont(name: "Helvetica Neue Light Italic", size: 35)
Then I render the textview in context by adding it as a subview to the parent view and then I get the image for export:
parentView.addSubview(textView) parentView.layer.renderInContext(UIGraphicsGetCurrentContext()) var newIMG = UIGraphicsGetImageFromCurrentImageContext()
The problem is that the height is fixed to 350px, but sometimes the text is not long enough to use all that area. Ideally I would like to know how many lines will my text use, so I can give it a proportional height and not have a white unused area (see pic for example):