简体   繁体   English

如何垂直实现自动调整大小的UITextView?

[英]how to implement the autoresizing UITextView vertically?

First look at the screenshot: 首先看截图: 在此处输入图片说明

second press the return button, the screenshot for your reference: 第二次按下返回按钮,屏幕截图供您参考: 在此处输入图片说明

在此处输入图片说明

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---- 非常感谢 - -

Make it UITextView not a text field, track the text changes at shouldChangeTextInRange of the textView delegate. 让它UITextView不是一个文本字段,在跟踪文本更改shouldChangeTextInRange的的textView代表。

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. 在此方法中,计算行数(textView.contentSize.height / textView.font.lineHeight),将其与上一个值进行比较,以确定是继续还是返回。 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 . 如果更改了行数,请检查textView是否已经显示最大行数(在图像上必须为3),然后根据结果将更改textView ,它的superview和一个按钮框架(也可以使其易于动画制作),并可能使用scrollRangeToVisible将文本滚动到更改区域。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM