I wanted to know when a text is wrapped by the frame of the text view is there any delimiter with which we can identify whether the text is wrapped or not.
For insta
Improved and update Luke Chase's answer to Swift 5, XCode 11, iOS 13 to get text view number of lines and autoresize table view cell height.
You can use storyboard with static cell height to design it as you want. Make UITextView scroll enable: false (disable scroll).
In viewDidLoad add your estimated row height and your textView delegate.
override func viewDidLoad() {
super.viewDidLoad()
quoteTextView.delegate = self
tableView.estimatedRowHeight = 142
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
extension ViewController: UITextViewDelegate {
func textViewDidChange(_ textView: UITextView) {
// Refresh tableView cell
if textView.numberOfLines > 2 { // textView in storyboard has two lines, so we match the design
// Animated height update
DispatchQueue.main.async {
self.tableView?.beginUpdates()
self.tableView?.endUpdates()
}
}
}
}
extension UITextView {
var numberOfLines: Int {
// Get number of lines
let numberOfGlyphs = self.layoutManager.numberOfGlyphs
var index = 0, numberOfLines = 0
var lineRange = NSRange(location: NSNotFound, length: 0)
while index < numberOfGlyphs {
self.layoutManager.lineFragmentRect(forGlyphAt: index, effectiveRange: &lineRange)
index = NSMaxRange(lineRange)
numberOfLines += 1
}
return numberOfLines
}
}
-> Do not forgot to disable uitextview scroll. Cheers!<-
Preview