Logo Questions Linux Laravel Mysql Ubuntu Git Menu

why UITextView draws text in bad frame after resizing?

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;


before resize Before resize

after resizeAfter resize

like image 871
Povilas Avatar asked Mar 25 '23 03:03


1 Answers

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];
like image 176
Povilas Avatar answered Mar 28 '23 02:03