gpt4 book ai didi

ios - TextView 无法正确调整大小

转载 作者:行者123 更新时间:2023-11-30 11:51:26 24 4
gpt4 key购买 nike

我有一个表格 View 单元格,里面有一个 TextView :

class DetailsCell: UITableViewCell, UITextViewDelegate {


let detailsTextView: UITextView = {
let tv = UITextView()
tv.font = UIFont(name: "AvenirNext-Medium", size: 24)
tv.isScrollEnabled = false
tv.textColor = .white
tv.backgroundColor = .clear
tv.translatesAutoresizingMaskIntoConstraints = false
return tv
}()


func textViewDidChange(_ textView: UITextView) {
let addTodoViewController = AddTodoViewController()
addTodoViewController.begindEndUpdate()
}



override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: .default, reuseIdentifier: "DetailsCell")

addSubview(detailsTextView)
detailsTextView.topAnchor.constraint(equalTo: topAnchor, constant: 10).isActive = true
detailsTextView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
detailsTextView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -10).isActive = true
detailsTextView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 10).isActive = true


backgroundColor = .black

textLabel?.isHidden = true

detailsTextView.delegate = self
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

当我加载应用程序并且 TextView 已经有一个以编程方式分配的文本时,单元格正确获得了正确的高度并且 TextView 正确调整了大小,但是当我尝试更改 TextView 的文本时,单元格和 TextView 不要调整大小。有人能帮我吗?

 override func viewWillAppear(_ animated: Bool) {
toDoTextField.becomeFirstResponder()
addTodoTableView.estimatedRowHeight = 60
addTodoTableView.rowHeight = UITableViewAutomaticDimension
}

Textview with not initial text Image

Textview with initial text Image

最佳答案

UITextView 在内容更改时不会调整自身大小。您必须编写一些代码来调整 textView 的大小,然后相应地调整单元格的高度。也许这可以帮助你resizing textview to its content

或者您可以使用一些第三方库来代替默认的UITextView,例如GrowingTextView

关于ios - TextView 无法正确调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48289828/

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