Hi,
I was wondering, if it is possible to set the height of a TextAreaRow to an automatic height based on the content. Usually I do this with content hugging priorities etc. in a storyboard, but your cell.height is of type (() -> CGFloat)? so if I want to change the height I have to pass a closure which calculates it?
Best regards
This is what I did to solve this:
CustomFormViewController.swift:
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardDidChangeFrame:", name: UIKeyboardWillChangeFrameNotification, object: nil)
form
+++ Section()
<<< TextAreaRow("Story") { row in
row.placeholder = row.tag
}
.onChange{ [weak self] row in
row.cell.height = {
let textViewHeight = row.cell.textView.sizeThatFits(CGSize(width: row.cell.textView.bounds.width, height: CGFloat.max)).height
var maxHeight: CGFloat = 250.0
if let navigationBarHeight = self?.navigationController?.navigationBar.bounds.height,
let keyboardHeight = self?.keyboardHeight {
maxHeight = UIScreen.mainScreen().bounds.height - navigationBarHeight - keyboardHeight
}
return textViewHeight.clamped(min: 110.0, max: maxHeight)
}
if let thisIndexPath = self?.tableView?.indexPathForCell(row.cell) {
self?.tableView?.scrollToRowAtIndexPath(thisIndexPath, atScrollPosition: .Bottom, animated: false)
}
self?.tableView?.beginUpdates()
self?.tableView?.endUpdates()
}
}
// ...
func keyboardDidChangeFrame(notification: NSNotification) {
let keyboardScreenEndFrame = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
let keyboardViewEndFrame = view.convertRect(keyboardScreenEndFrame, fromView: view.window)
keyboardHeight = keyboardViewEndFrame.height
}
Comparable+Extension.swift:
extension Comparable {
func clamped(min min: Self, max: Self) -> Self {
if self < min {
return min
}
if self > max {
return max
}
return self
}
}
Most helpful comment
This is what I did to solve this:
CustomFormViewController.swift:
Comparable+Extension.swift: