使用contentScaleFactor属性缩放UITextView

时间:2011-05-08 11:45:34

标签: objective-c iphone uiview uitextview scaling

我正在尝试创建UIView的更高分辨率图像,特别是UITextView

这个问题和答案正是我想要弄清楚的:

但是,当我这样做时,我的文字仍然模糊:

self.view.transform = CGAffineTransformMakeScale(2.f, 2.f);
[myText setContentScaleFactor:2.f]; // myText is a subview of self.view object

我也在Apple示例项目“UICatalog”中对UILabel进行了同样的操作,但它也很模糊。

我无法理解为什么它会从另一个问题中为Warrior工作而不适合我。我会问那里 - 但我似乎无法在那里留下评论或问题。

3 个答案:

答案 0 :(得分:7)

设置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];
    }
}

答案 1 :(得分:1)

UITextView具有textInputView属性,“既绘制文本又提供坐标系”(https://developer.apple.com/reference/uikit/uitextinput/1614564-textinputview

所以我使用以下代码来缩放UITextView - 没有任何字体更改,不使用任何“CALayer”属性并保持高质量:

float screenScaleFactor = [[UIScreen mainScreen] scale];
float scale = 5.0;
textView.transform = CGAffineTransformMakeScale(scale, scale);
textView.textInputView.contentScaleFactor = screenScaleFactor * scale;

如果您需要低质量(但性能更好)缩放,请注释最后一行。

变换使用view.center作为缩放中心点,因此需要添加“平移变换”来缩放视角。

答案 2 :(得分:0)

确保将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];
    }
}