the textview height increase by using pure swift code design. I am just do coding part only.
I take this idea from
https://stackoverflow.com/a/45071002/9110213
First thing create the textView
import UIKit
class TextFieldCell: UITableViewCell {
lazy var btnEdit: UIButton! = {
let button = UIButton()
button.translatesAutoresizingMaskIntoConstraints = false
button.addTarget(self, action: #selector(self.actionEdit(_:)), for: .touchUpInside)
button.setTitle("Edit", for: .normal)
button.titleLabel?.font = UIFont(name: "ProximaNova-Medium", size: 18)
button.setTitleColor( UIColor(red: 0.29, green: 0.56, blue: 0.89, alpha: 1), for: .normal)
button.titleLabel?.textAlignment = .left
return button
}()
lazy var separatorView: UIView! = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
lazy var textView: UITextView! = {
let textView = UITextView.init(frame: .zero)
textView.translatesAutoresizingMaskIntoConstraints = false
textView.delegate = self
textView.isScrollEnabled = false
return textView
}()
lazy var titleLabel: UILabel! = {
let label = UILabel(frame: CGRect(x: 27, y: 318, width: 27, height: 12))
label.text = "Name"
label.font = UIFont(name: "ProximaNova-Medium", size: 10)
label.textColor = UIColor(red: 0.61, green: 0.61, blue: 0.61, alpha: 1)
label.textAlignment = .left
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
deinit {
self.titleLabel = nil
self.textView = nil
self.separatorView = nil
self.btnEdit = nil
}
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.selectionStyle = .none
self.addView()
self.setConstraint()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
extension TextFieldCell {
func addView(){
self.contentView.addSubview(self.btnEdit)
self.contentView.addSubview(self.titleLabel)
self.contentView.addSubview(self.textView)
self.contentView.addSubview(self.separatorView)
}
func setConstraint(){
// This part is very important to increase the textview height dyamically
let textViewHeight = self.textView.heightAnchor.constraint(equalToConstant: 27)
textViewHeight.priority = .defaultHigh
self.textView.setContentCompressionResistancePriority(.required, for: .vertical)
self.textView.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
NSLayoutConstraint.activate([
self.titleLabel.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: 27),
self.titleLabel.topAnchor.constraint(equalTo: self.contentView.topAnchor, constant: 38),
self.textView.topAnchor.constraint(equalTo: self.titleLabel.bottomAnchor, constant: 9),
self.textView.leadingAnchor.constraint(equalTo: self.titleLabel.leadingAnchor),
textViewHeight,
self.textView.trailingAnchor.constraint(equalTo: self.btnEdit.leadingAnchor, constant: -25),
self.btnEdit.centerYAnchor.constraint(equalTo: self.textView.centerYAnchor),
self.btnEdit.widthAnchor.constraint(equalToConstant: 40),
self.btnEdit.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor, constant: -33),
self.separatorView.topAnchor.constraint(equalTo: self.textView.bottomAnchor, constant: 10),
self.separatorView.heightAnchor.constraint(equalToConstant: 1),
self.separatorView.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor),
self.separatorView.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: 27),
self.separatorView.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor, constant: -15),
])
}
}
extension TextFieldCell {
@objc func actionEdit(_ sender: UIButton) {
}
}
extension TextFieldCell: UITextViewDelegate {
func notifyViewController(text:String){
}
func textViewDidEndEditing(_ textView: UITextView) {
}
}