UITextView and contentScaleFactor

I have a number of text paths in scrollView that can be enlarged. To redraw the controls with a higher resolution to avoid blurry text, I set each kind of contentScaleFactor in the view hierarchy, as described here . Everything works fine for shortcuts and text fields, but textViews do not redraw with a higher scale factor. I noticed that the only other subview for textView that can make a difference if set is a private UIWebDocumentView that implements content like UIWebView (i.e. WebKit), but the new scale factor is ignored if it is set at any level (UITextView or UIWebDocumentView).

Any ideas on how to reset the scale factor (resolution) for TextViews specifically?

+4
source share
2 answers

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]; } } 
+3
source

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, you also need to consider the scale of the screen.

The implementation will 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]; } } 

You can call this when zooming in (part of UIScrollViewDelegate ):

 - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale { [self updateForZoomScale:scale]; } 

I filed a promotion request here: rdar: // 21443666 ( http://www.openradar.me/21443666 ). There is also an example project with a workaround attached.

+3
source

Source: https://habr.com/ru/post/1486729/


All Articles