Swift 3 & Xcode 8.3.2
Verwenden UILabel anstelle von UITextView und setzen numberOfLine = 0 ist, so wird es automatisch Resize entsprechend seinem Inhalt
oder
wenn Sie UITextView statt UILabe behalten möchten l, hier ist der Code
class YourClass: UITableViewController, UITextViewDelegate {
var yourCustomCell: UITableViewCell = UITableViewCell()
override func viewDidLoad() {
super.viewDidLoad()
table.estimatedRowHeight = 40.0 // Replace with your actual estimation
table.rowHeight = UITableViewAutomaticDimension
// Tap to dismiss keyboard
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(EditInfoViewController.dismissKeyboard))
view.addGestureRecognizer(tap)
// Add tableView delegate
tableView.dataSource = self
tableView.delegate = self
// Add textView delegate
yourTextView.delegate = self
}
// Text view delegate, dont forget to add yourTextView.delegate = self in viewDidLoad
func textViewDidChange(_ textView: UITextView) {
if textView == yourTextView {
let newHeight = yourCustomCell.frame.size.height + textView.contentSize.height
yourCustomCell.frame.size.height = newHeight
updateTableViewContentOffsetForTextView()
}
}
// Animate cell, the cell frame will follow textView content
func updateTableViewContentOffsetForTextView() {
let currentOffset = tableView.contentOffset
UIView.setAnimationsEnabled(false)
tableView.beginUpdates()
tableView.endUpdates()
UIView.setAnimationsEnabled(true)
tableView.setContentOffset(currentOffset, animated: false)
}
// UITableViewDelegate, UITableViewDataSource
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return UITableViewAutomaticDimension
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = yourCustomCell
cell.selectionStyle = .none
return cell
}
}
Das Ergebnis ist hier:
Ergebnis nach textViewDelegate verwenden und individuelle Größenanpassung Funktion
Ich weiß nicht, wie ein Etikett machen edditable so Ich habe versucht, die textView-Größe zu ändern. Ich bin mir nicht sicher, was ich falsch mache. So setze ich den delegierten TextView: titleTextView.delegate = self as? UITextViewDelegate – alvarlagerlof
@Ideaman in Ihrer AnsichtDidLoad deklarieren Sie diese "titleTextView.delege = self", und in Ihrer Klasse fügen Sie diese Klasse myViewController: UIViewController, UITextViewDelegate {} –
Es möchte ich es in UITextViewDelegate – alvarlagerlof