Friday, 11 September 2015

Animate UITextView With Text

//First set maximum height for UITextView
maximumHeight = 120;

//Add UITextView
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 50, 300, 40)];
    textView.delegate = self;
    textView.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:textView];


// Delegate of UItextView
-(void) textViewDidChange:(UITextView *)textView{

    UITextPosition* pos = textView.endOfDocument;//explore others like beginningOfDocument if you want to customize the behaviour
    CGRect currentRect = [textView caretRectForPosition:pos];

        CGRect  newFrame = textView.frame;
        newFrame.size.height = currentRect.size.height+currentRect.origin.y+5;
        if (newFrame.size.height>40 && newFrame.size.height<=maximumHeight) {
            [UIView beginAnimations:@"" context:nil];
            [UIView setAnimationDuration:0.1f];
            textView.frame = newFrame;
            [UIView commitAnimations];
        }
        else if (newFrame.size.height<40){
            newFrame.size.height=40;
            [UIView beginAnimations:@"" context:nil];
            [UIView setAnimationDuration:0.1f];
            textView.frame = newFrame;
            [UIView commitAnimations];
        }
    }

No comments:

Post a Comment