How to increase width of textfield according to typed text?

前端 未结 8 1923
失恋的感觉
失恋的感觉 2020-12-09 04:22

I need to increase a text field\'s width according to its content. When the user inputs text, then the textfield size should increase automatically. I have one close (X) but

8条回答
  •  半阙折子戏
    2020-12-09 04:58

    I think this is a better solution than having a width constraint that you have to modify:

    Resize a UITextField while typing (by using Autolayout)

    - (IBAction) textFieldDidChange: (UITextField*) textField
    {
        [UIView animateWithDuration:0.1 animations:^{
            [textField invalidateIntrinsicContentSize];
        }];
    }
    

    You can omit the animation if desired..

    EDIT: Here's an example project: https://github.com/TomSwift/growingTextField

提交回复
热议问题