Scaling a UITextView using the contentScaleFactor property

I am trying to create a high resolution image of a UIView, specifically a UITextView.

This question and answer is exactly what I'm trying to figure out:

stack overflow

But, when I do the same, my text is still blurry:

self.view.transform = CGAffineTransformMakeScale(2.f, 2.f); [myText setContentScaleFactor:2.f]; // myText is a subview of self.view object 

I also tried the same in the Apple UICatalog sample project for UILabel, and it is also blurry.

I can’t understand why this will work for the Warrior from another question, and not for me. I would ask, but I can not leave a comment or question there.

Any suggestions would be greatly appreciated, thanks.

+2
iphone uiview uitextview scaling
source share
3 answers

Setting contentScaleFactor and contentsScale is actually the key, as @dbotha pointed out, however you need to go through the view and level hierarchy separately to cover all the internal CATiledLayer that actually render the text, Adding a screen scale can also make sense.

So, the correct implementation would be something like this:

 - (void)updateForZoomScale:(CGFloat)zoomScale { CGFloat screenAndZoomScale = zoomScale * [UIScreen mainScreen].scale; // Walk the layer and view hierarchies separately. We need to reach all tiled layers. [self applyScale:(zoomScale * [UIScreen mainScreen].scale) toView:self.textView]; [self applyScale:(zoomScale * [UIScreen mainScreen].scale) toLayer:self.textView.layer]; } - (void)applyScale:(CGFloat)scale toView:(UIView *)view { view.contentScaleFactor = scale; for (UIView *subview in view.subviews) { [self applyScale:scale toView:subview]; } } - (void)applyScale:(CGFloat)scale toLayer:(CALayer *)layer { layer.contentsScale = scale; for (CALayer *sublayer in layer.sublayers) { [self applyScale:scale toLayer:sublayer]; } } 
+4
source share

UITextView has a textInputView property that "both draws text and provides a coordinate system" ( https://developer.apple.com/reference/uikit/uitextinput/1614564-textinputview )

So, I use the following code to scale the UITextView - without any font changes, without using any “CALayer” property and maintaining high quality:

 float screenScaleFactor = [[UIScreen mainScreen] scale]; float scale = 5.0; textView.transform = CGAffineTransformMakeScale(scale, scale); textView.textInputView.contentScaleFactor = screenScaleFactor * scale; 

Comment on the last line if you need to scale with poor quality (but better).

Transform uses view.center as the center point of the zoom, so adding a “transform transform” is necessary to scale around the viewing angle.

+1
source share

Be sure to apply contentScaleFactor to all UITextView subzones. I just checked the following with a UITextView and found that it works:

 - (void)applyScale:(CGFloat)scale toView:(UIView *)view { view.contentScaleFactor = scale; view.layer.contentsScale = scale; for (UIView *subview in view.subviews) { [self applyScale:scale toView:subview]; } } 
0
source share

All Articles