How to get a selected range of text from a UITextView or UITextField
Asked Answered
V

4

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);

}
Valvular answered 26/3, 2013 at 5:59 Comment(0)
S
16

You can try this,

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

   UITextRange *selectedRange = [textView selectedTextRange];
   NSString *selectedText = [textView textInRange:selectedRange];
}
Serenity answered 12/8, 2013 at 7:14 Comment(0)
F
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.
Forevermore answered 8/6, 2016 at 2:37 Comment(0)
D
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
}

Diann answered 19/9, 2019 at 8:23 Comment(0)
F
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...

Forensic answered 23/7, 2018 at 8:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.