使用 contentScaleFactor 属性缩放 UITextView [英] Scaling UITextView using contentScaleFactor property

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

问题描述

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

I am trying to create a higher resolution image of a UIView, specifically UITextView.

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

This question and answer is exactly what I am trying to figure out:

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

But, when I do the same, my text is still blurry:

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

我也在 Apple 示例项目UICatalog"中尝试了相同的方法到 UILabel 并且它也很模糊.

I have also tried the same in the Apple sample project "UICatalog" to UILabel and it is also blurry.

我无法理解为什么它对另一个问题的 Warrior 有效,而不对我有效.我会在那里问 - 但我似乎无法在那里发表评论或问题.

I can't understand why it would work for Warrior from the other question and not for me. I would have asked there — but I can't seem to leave a comment or a question there.

推荐答案

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

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. Adding the screen scale might also make sense.

所以正确的实现应该是这样的:

So the correct 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];
    }
}

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

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