6

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

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

4

2 回答 2

4

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

实现将是这样的:

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

当缩放比例发生变化时,您可以调用它(部分UIScrollViewDelegate):

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

我在这里提交了增强请求:rdar://21443666 ( http://www.openradar.me/21443666 )。还有一个带有解决方法的示例项目。

于 2015-06-18T17:45:18.297 回答
3

确保将 contentScaleFactor 应用于 UITextView 的所有子视图。我刚刚用 UITextView 测试了以下内容,发现它可以工作:

- (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];
    }
}
于 2013-11-20T23:24:47.293 回答