正如@dbotha 指出的那样,设置contentScaleFactor
and实际上是关键,但是您必须分别遍历视图和层层次结构才能到达实际执行文本渲染的每个内部。您还需要考虑屏幕比例。contentsScale
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 )。还有一个带有解决方法的示例项目。