How to move cursor of UITextView to TOUCHED LOCATION?

前端 未结 2 963
予麋鹿
予麋鹿 2021-01-01 04:35

The goal of my question is making note app like iOS\'s. The point of iOS\'s note app is that they provide to user to use hyper link or phone number link or email address as

相关标签:
2条回答
  • 2021-01-01 04:48

    Above Answer in Swift 4:

    tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.textViewTapped(recognizer:)))
    textView.addGestureRecognizer(tapGesture)
    
    @objc func textViewTapped(recognizer: UITapGestureRecognizer) {
        if recognizer.state == .ended {     
            textView.isEditable = true
            textView.becomeFirstResponder()
    
            let location = recognizer.location(in: textView)
            if let position = textView.closestPosition(to: location) {
                let uiTextRange = textView.textRange(from: position, to: position)
    
                if let start = uiTextRange?.start, let end = uiTextRange?.end {
                    let loc = textView.offset(from: textView.beginningOfDocument, to: position)
                    let length = textView.offset(from: start, to: end)
    
                    textView.selectedRange = NSMakeRange(loc, length)
    
                }
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-01 04:55
    - (void) editTextRecognizerTabbed:(UITapGestureRecognizer *) aRecognizer
    {
        if (aRecognizer.state==UIGestureRecognizerStateEnded)
        {
            //Not sure if you need all this, but just carrying it forward from your code snippet
            textview.editable = YES;
            textview.dataDetectorTypes = UIDataDetectorTypeNone;
            [textview becomeFirstResponder];
    
            //Consider replacing self.view here with whatever view you want the point within
            CGPoint point = [aRecognizer locationInView:self.view];
            UITextPosition * position=[textView closestPositionToPoint:point];
            [textView setSelectedTextRange:[textView textRangeFromPosition:position toPosition:position]];
        }
    }
    
    0 讨论(0)
提交回复
热议问题