【问题标题】:UITextView - Begin Editing With All Text Selected In UITableViewCell()UITextView - 使用 UITableViewCell() 中选择的所有文本开始编辑
【发布时间】:2017-03-15 05:45:01
【问题描述】:

我有一个带有对象TaskCell 的tableView,它是UITableViewCell 的子类

textViewTaskCell 中,一切正常,除了我无法让这行代码选择文本视图中的所有文本,因此用户可以根据需要立即覆盖文本。就像您在按住文本后点击“全选”一样。

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
}

【问题讨论】:

    标签: ios swift uitextview uitextviewdelegate


    【解决方案1】:

    你有两个选择:

     [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)
        }
    }
    

    【讨论】:

    • 它对我来说很简单,你应该在你有i.stack.imgur.com/sGdSf.png的情况下应用它
    • 它例如在 textViewDidChange 中工作,只要我输入一些内容,它就会突出显示所有文本。但只是不在 textViewDidBeginEditing 中。
    • DispatchQueue.main.async 修复了它!在 shouldBeginEditing 的情况下,它会在 textViewDidEndEditing 上导致一些奇怪的无限循环错误。
    • 再次感谢@Alsh 编译器
    猜你喜欢
    • 2014-02-12
    • 1970-01-01
    • 2010-11-27
    • 2015-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-25
    • 1970-01-01
    相关资源
    最近更新 更多