我有一个带有对象TaskCell
的tableView,它是UITableViewCell
textView
位于TaskCell
,一切正常,但我无法通过这行代码选择文本视图中的所有文本,以便用户可以立即覆盖文本。就像你要点击"选择所有"在抓住文本的水龙头之后。
textView.selectedTextRange = textView.textRange(from: textView.beginningOfDocument, to: textView.endOfDocument)
_
// All code relating to textView
class TaskCell: UITableViewCell {
@IBOutlet weak var textView: UITextView! { didSet { initTextView() } }
fileprivate func initTextView() {
textView.delegate = self
}
}
extension TaskCell: UITextViewDelegate {
func textViewDidBeginEditing(_ textView: UITextView) {
textView.selectedTextRange = textView.textRange(from: textView.beginningOfDocument, to: textView.endOfDocument)
}
}
// class ListViewController: ViewController, UITableViewDataSource, UITableViewDelegate
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TaskCell
return cell
}
答案 0 :(得分:2)
您有两种选择:
[yourtextView selectAll:nil]; //highlights text
[yourtextView selectAll:self]; //highlights text and shows menu(cut copy paste)
在你的情况下,这将解决它:
- (BOOL)textViewDidBeginEditing:(UITextView *)textView {
dispatch_async(dispatch_get_main_queue(), ^{
[textView selectAll:nil];
});
return YES;
}
Swift 3.0
func textViewDidBeginEditing(_ textView: UITextView) {
DispatchQueue.main.async {
textView.selectAll(nil)
}
}