13

I am trying to get the selected range of text from a UITextView (and or UITextField) so that I can edit the selected text, or modify an attributed string. The method below is triggered when I make a selection, but the code in the method returns null values.

- (void)textViewDidChangeSelection:(UITextView *)textView {

    UITextRange *selectedRange = [textField selectedTextRange];

    NSLog(@"Start: %@ <> End: %@", selectedRange.start, selectedRange.end);

}

4 Answers 4

16

You can try this,

- (void)textViewDidChangeSelection:(UITextView *)textView {

   UITextRange *selectedRange = [textView selectedTextRange];
   NSString *selectedText = [textView textInRange:selectedRange];
}
8

Swift

First get the selected text range, and then use that range to get the actual text:

if let textRange = myTextView.selectedTextRange {

    let selectedText = myTextView.text(in: textRange)

    // ...
}

Notes:

  • Selecting text from a UITextField is done in the same way.
  • The range is a UITextRange, not an NSRange. This allows for proper selection of things like emoji and extended grapheme clusters. See this answer for related details about this.
2

Swift 5.0

here is how I selecte file name Panda from Panda.txt

func textFieldDidBeginEditing(_ textField: UITextField) {
    // if textField.text is `Panda.txt`, offset will be 3
    let offset = String(textField.text!.split(separator: ".").last!).length
    let from = textField.position(from: textField.beginningOfDocument, offset: 0)
    let to = textField.position(from: textField.beginningOfDocument,
                                offset:textField.text!.length - (offset+1) )
    //now `Panda` will be selected
    textField.selectedTextRange = textField.textRange(from: from!, to: to!)
    //note: unwrap with `!` is not recommended, text here is 100% not null,so it's safe
}

0

Swift 4.1

In your UITextView Extension put the below code in a function, and use that in your controller: You can call this method with your textView instance in the SelectionDidChange delegate method from your view-Controller. Better to wrap this function call with condition textView.selectedRange.length > 0, to get some text...

  let begin = self.beginningOfDocument
  let start = self.position(from: begin, offset: selectedRange.location)
  let end = self.position(from: position(from: start!, offset: 0)!, offset: selectedRange.length)
  let txtRange = self.textRange(from: start!, to: end!)

  let txt = self.text(in: txtRange!)
  print("Sel Text is \(String(describing: txt))")

We can not use the optional binding to store the selected range, instead you can declare an optional for nsrange type, then use the if- let ... thing.

TextInputComponent has a property to get the selected text range.

let range = textView.selectedRange

Then you can use range.location, range.length values to change attributes of text in the container, etc...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.