如果我在viewDidLoad中缩放UITextView,字体会变得模糊

时间:2019-01-22 10:16:35

标签: ios objective-c uitextview

我在viewDidLoad中创建了一个UITextView,设置一些文本并缩放它。但是,即使更改contentScaleFactor,文本也非常模糊。但是,如果我添加另一个按钮来再次设置contentScaleFactor,则文本将变得非常清晰。有什么办法可以解决这个问题?我正在iOS 12中运行此代码。

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.view.backgroundColor = [UIColor whiteColor];
    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectZero];
    textView.font = [UIFont systemFontOfSize:20.f];
    textView.text = @"Happy";
    textView.textAlignment = NSTextAlignmentCenter;
    textView.backgroundColor = [UIColor yellowColor];
    textView.textColor = [UIColor blackColor];
    [self.view addSubview:textView];
    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.view);
        make.top.equalTo(self.view).offset(480);
        make.height.mas_equalTo(30);
    }];
    self.textView = textView;
    textView.transform = CGAffineTransformScale(textView.transform, 8, 8);
    [self applyScale:8];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.backgroundColor = [UIColor redColor];
    [self.view addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).offset(-10);
        make.centerX.equalTo(self.view);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(50);
    }];
    [button addTarget:self action:@selector(onTapBtn) forControlEvents:UIControlEventTouchUpInside];
}

- (void)onTapBtn {
    [self applyScale:8];
}

- (void)applyScale:(CGFloat)scale {
    [self applyScale:scale toView:self.textView];
}

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

0 个答案:

没有答案