UITextView 和 contentScaleFactor [英] UITextView and contentScaleFactor

查看:24
本文介绍了UITextView 和 contentScaleFactor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在滚动视图中有许多可以缩放的文本控件.为了以更高的分辨率重绘控件以避免文本模糊,我在视图层次结构中设置了每个视图的 contentScaleFactor,如此处. 标签和文本字段一切正常,但 textViews 不会以更高的比例因子重绘.我注意到 textViews 的唯一其他子视图可能会有所不同,如果 set 是私有类 UIWebDocumentView,它实现了 UIWebView(即 WebKit)等内容,但如果在任一级别( UITextView 或 UIWebDocumentView )设置,新的比例因子将被忽略.

任何想法如何专门为 TextViews 重置比例因子(分辨率)?

解决方案

设置 contentScaleFactorcontentsScale 实际上是关键,正如@dbotha 指出的那样,但是你必须分别遍历视图和图层层次结构才能到达实际执行文本渲染的每个内部 CATiledLayer.您还需要考虑屏幕比例.

实现将是这样的:

- (void)updateForZoomScale:(CGFloat)zoomScale {CGFloat screenAndZoomScale = zoomScale * [UIScreen mainScreen].scale;//分别遍历图层和查看层次结构.我们需要到达所有平铺层.[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 = 比例;for (UIView *view.subviews 中的子视图) {[self applyScale:scale toView:subview];}}- (void)applyScale:(CGFloat)scale toLayer:(CALayer *)layer {layer.contentsScale = 比例;for (CALayer *sublayer in layer.sublayers) {[self applyScale:scale toLayer:sublayer];}}

您可以在缩放比例改变时调用它(UIScrollViewDelegate 的一部分):

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

我在此处提交了增强请求:rdar://21443666 (http://www.openradar.me/21443666).还有一个附有变通方法的示例项目.

I have a number of text contols in a scrollView that can be zoomed. In order to redraw the controls at a higher resolution to avoid blurry text, I set each view's contentScaleFactor in the view hierarchy as explained here. Everything works fine for labels and textfields but textViews do not redraw at the higher scale factor. I noticed that the only other subview for textViews that may make a difference if set is a private class UIWebDocumentView which implements content like UIWebView ( ie WebKit) but the new scale factor is ignored if set at either level ( UITextView or UIWebDocumentView ).

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

解决方案

Setting the contentScaleFactor and contentsScale is in fact the key, as @dbotha pointed out, however you have to walk the view and layer hierarchies separately in order to reach every internal CATiledLayer that actually does the text rendering. You also need to account for the screen scale.

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

You can than call this when the zoom scale changes (part of UIScrollViewDelegate):

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

I filed an enhancement request here: rdar://21443666 (http://www.openradar.me/21443666). There's also a sample project with the workaround attached.

这篇关于UITextView 和 contentScaleFactor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆