为什么 UITextView 在调整大小后在坏帧中绘制文本? [英] why UITextView draws text in bad frame after resizing?

查看:18
本文介绍了为什么 UITextView 在调整大小后在坏帧中绘制文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被某种魔法困住了:当我尝试更改 UITextView 框架(在这种情况下使用 UISlider)时,文本是在框架之外的其他(较小)区域中绘制的(多次调整大小).有趣的是,如果我们在尝试使框架更大时滑动足够快,文本会被绘制在非常正确的区域.有人可以解释为什么吗?我尝试布局、自动调整大小、设置内容大小和边界,但没有任何效果.

I'm stuck with some kind of magic: when I try to change UITextView frame (in this case with UISlider), text is drawn in some other (smaller) area than frame (resizing multiple times). The funny thing is that if we slide fast enough when trying to make frame bigger, text is drawn in pretty correct area. Can somebody explain why? I tried to layout, autoresize, set content size and bounds, but nothing works.

- (void)viewDidLoad
{
    [super viewDidLoad];

    _textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 10, 100, 100)];
    _textView.backgroundColor = [UIColor greenColor];
    _textView.text = @"some useless text which will be drawn bad";
    [self.view addSubview:_textView];

    UISlider *slide = [[UISlider alloc] initWithFrame:CGRectMake(10, 130, 100, 20)];
    [slide setValue:0.0];
    [slide addTarget:self action:@selector(changedValue:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:slide];
}
- (void)changedValue:(UISlider *)slider
{
    CGRect textViewFrame = _textView.frame;
    textViewFrame.size.width = [slider value] * 200;
    _textView.frame = textViewFrame;

}

调整大小前

调整大小后

推荐答案

好吧,经过几个小时的 google,我发现在设置我计算的帧值之前设置 CGRectZero 帧可以消除我的问题.(我子类化 UITextView 以覆盖 setFrame: 方法).我的代码如下.

Well, after several hours of google, I find out that setting CGRectZero frame before setting my calculated frame value eliminates my problem. (I subclassed UITextView to override setFrame: method). My code bellow.

- (void)setFrame:(CGRect)frame
{
    [super setFrame:CGRectZero];
    [super setFrame:frame];
}

这篇关于为什么 UITextView 在调整大小后在坏帧中绘制文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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