UITextView 最后一行位置
UITextView last line position
我遇到了下一个问题:我有一个 UITableView
,其中的自定义单元格有一个 UITextView
。用户开始打字后,最后一行与键盘重叠,键盘有输入附件视图。所以我必须计算键盘顶部和最后一行 .y
位置之间的差异,并将此值分配给 tableView contentInset。
问题是如何找出文本视图最后一行的矩形或位置?
我有监听键盘的普通通知:
NotificationCenter.default.addObserver(self, selector: #selector(_toggleKeyboard), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(_toggleKeyboard), name: UIResponder.keyboardWillHideNotification, object: nil)
我必须计算所有这些东西的下一个方法:
@objc private func _toggleKeyboard(notification: NSNotification) {
let info = notification.userInfo as NSDictionary?
let rectValue = info![UIResponder.keyboardFrameEndUserInfoKey] as! NSValue
let kbSize = rectValue.cgRectValue
let kbFrame = view.convert(kbSize, from: view.window)
// val = bottom .y of last text view line
// keyboard .top - val = diff
// contentInsets.bottom = diff
}
终于找到了解决办法:
let textPosition = textView.endOfDocument
let rect = textView.caretRect(for: textPosition)
我遇到了下一个问题:我有一个 UITableView
,其中的自定义单元格有一个 UITextView
。用户开始打字后,最后一行与键盘重叠,键盘有输入附件视图。所以我必须计算键盘顶部和最后一行 .y
位置之间的差异,并将此值分配给 tableView contentInset。
问题是如何找出文本视图最后一行的矩形或位置?
我有监听键盘的普通通知:
NotificationCenter.default.addObserver(self, selector: #selector(_toggleKeyboard), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(_toggleKeyboard), name: UIResponder.keyboardWillHideNotification, object: nil)
我必须计算所有这些东西的下一个方法:
@objc private func _toggleKeyboard(notification: NSNotification) {
let info = notification.userInfo as NSDictionary?
let rectValue = info![UIResponder.keyboardFrameEndUserInfoKey] as! NSValue
let kbSize = rectValue.cgRectValue
let kbFrame = view.convert(kbSize, from: view.window)
// val = bottom .y of last text view line
// keyboard .top - val = diff
// contentInsets.bottom = diff
}
终于找到了解决办法:
let textPosition = textView.endOfDocument
let rect = textView.caretRect(for: textPosition)