本文介绍了自调整uitextview到特定高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个具有聊天功能的应用程序,其中 UITextview
用于输入消息. UITextview
的高度必须是动态的(如果用户输入消息,则必须根据文本长度更改高度,直到达到特定的高度).
I have an app which has chatting functionality where UITextview
is used for entering the message. UITextview
height has to be dynamic (if user enters the message, the height has to be changed according to the text length till a specific Height).
我该如何实现?
推荐答案
禁用textView滚动.
Disable Scrolling of textView.
将高度增加到特定值,然后启用滚动.
TO Increase Height to a specific value and then enable scrolling.
提供最大高度限制,然后将此代码添加到您的viewController
Provide a maximum height constraint then add this code to your viewController
class YourViewController: UIViewController, UITextViewDelegate
{
@IBOutlet weak var yourTextView: UITextView!
let textViewMaxHeight: CGFloat = 100
override func viewDidLoad()
{
super.viewDidLoad()
yourTextView.delegate = self
}
func textViewDidChange(textView: UITextView)
{
if textView.contentSize.height >= self.textViewMaxHeight
{
textView.scrollEnabled = true
}
else
{
textView.frame.size.height = textView.contentSize.height
textView.scrollEnabled = false
}
}
}
这篇关于自调整uitextview到特定高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!