UITextView和contentScaleFactor

时间:2013-06-18 02:42:40

标签: zoom uitextview

我在scrollView中有一些可以缩放的文本控件。为了以更高的分辨率重绘控件以避免模糊文本,我按照 here的说明在视图层次结构中设置每个视图的contentScaleFactor。一切都适用于标签和文本字段但textViews不能重绘较高比例因子。我注意到textViews的唯一其他子视图可能会有所不同,如果set是一个私有类UIWebDocumentView,它实现像UIWebView(即WebKit)这样的内容,但如果设置在任一级别(UITextView或UIWebDocumentView),则忽略新的比例因子。 p>

有关如何重置TextViews的比例因子(分辨率)的任何想法吗?

2 个答案:

答案 0 :(得分:4)

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

实施将是这样的:

- (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)。还有一个附带解决方法的示例项目。

答案 1 :(得分: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];
    }
}