gpt4 book ai didi

ios - 自适应 uitextview 直到特定高度

转载 作者:可可西里 更新时间:2023-10-31 23:56:12 34 4
gpt4 key购买 nike

我有一个具有聊天功能的应用程序,其中 UITextview 用于输入消息。 UITextview 高度必须是动态的(如果用户输入消息,高度必须根据文本长度更改,直到特定高度)。

我怎样才能做到这一点?

最佳答案

禁用 textView 的滚动。

enter image description here

enter image description here

将高度增加到特定值,然后启用滚动。

提供最大高度限制 然后将此代码添加到您的 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
}
}
}

关于ios - 自适应 uitextview 直到特定高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48622024/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com