user1279968
user1279968

Reputation: 141

how to implement the autoresizing UITextView vertically?

First look at the screenshot: enter image description here

second press the return button, the screenshot for your reference: enter image description here

enter image description here

after four times click

return

button, the height of text field will not be higher any more.

now here is the question: how can I implement the function of textfield? and what about the background Image behind the text Field , should auto resize too? if there are any solution, let me know! thanks very much----

Upvotes: 1

Views: 753

Answers (1)

A-Live
A-Live

Reputation: 8944

Make it UITextView not a text field, track the text changes at shouldChangeTextInRange of the textView delegate.

In this method calculate the number of lines (textView.contentSize.height/textView.font.lineHeight), compare it with the previous value to decide whether you need to proceed or just return. If the number of lines is changed, check whether the textView is already displaying the maximum number of lines (that must be 3 on the image) and based on the results you will change the textView, it's superview and a button frames (you can also make it animated easily) and probably scroll the text to the change region with scrollRangeToVisible.

Upvotes: 1

Related Questions