is it possible to get the index (String.Index) value from the cursor position of a UITextView element in Swift?

DaveJ

I'm looking to pull the index value (String.Index) from the user cursor position of a UITextView element. I'm using the selectedTextRange method to get the UITextRange value. How can I use that to retrieve the index value?

Leo Dabus

You can get the selected range offset from the beginning of your document to the start of the selected range and use that offset to get your string index as follow:

extension UITextView {
    var cursorOffset: Int? {
        guard let range = selectedTextRange else { return nil }
        return offset(from: beginningOfDocument, to: range.start)
    }
    var cursorIndex: String.Index? {
        guard let location = cursorOffset else { return nil }
        return Range(.init(location: location, length: 0), in: text)?.lowerBound
    }
    var cursorDistance: Int? {
        guard let cursorIndex = cursorIndex else { return nil }
        return text.distance(from: text.startIndex, to: cursorIndex)
    }
}

class ViewController: UIViewController, UITextViewDelegate {
    @IBOutlet weak var textView: UITextView!
    override func viewDidLoad() {
        super.viewDidLoad()
        textView.delegate = self
    }
    func textViewDidChangeSelection(_ textView: UITextView) {
        print("Cursor UTF16 Offset:",textView.cursorOffset ?? "")
        print("Cursor Index:", textView.cursorIndex ?? "")
        print("Cursor distance:", textView.cursorDistance ?? "")
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Swift: Get double value from 'index' in enumeration

From Java

Getting and Setting Cursor Position of UITextField and UITextView in Swift

From Dev

Is it possible to get the "coordinates" of an element based on it's index?

From Dev

How to get a specific character from index of a string in swift

From Dev

Get index position of where vector changes value

From Dev

Index position of an element

From Dev

Getting value from string by index

From Dev

Python Pandas value from index position

From Dev

Get a value at index from range

From Dev

Get value from gson with index

From Dev

Get word/character screen position by its string index in Kinetic.js Text element

From Dev

RichTextBox get string from Index

From Dev

Swift: Search for String inside String and get Index

From Dev

Get key from HashMap in Android by position or index

From Dev

How to get the index of Differences in String from index - to index?

From Dev

Is it possible to get value of an index with ArrayList<Comparable>?

From Dev

How to get the value of a Range given an index in Swift

From Dev

Is it possible to get numeric index (position) of marks in tkinter text?

From Dev

Is it possible to get numeric index (position) of marks in tkinter text?

From Dev

Pop value from array by index of clicked element

From Dev

How to get String with specific index - swift

From Dev

Delete array element if index position is greater than a specific value

From Dev

Is it thread safe to get an element by index from List

From Dev

Realm dotnet: get element by index from RealmResults

From Dev

How to get the index of element from body tag?

From Dev

Get index from different element group

From Dev

Get index of element from array on click Jquery

From Dev

Remove item from List<string[]> or get index of any item at any position

From Dev

How to get index, position number of element with certain class with jQuery

Related Related

  1. 1

    Swift: Get double value from 'index' in enumeration

  2. 2

    Getting and Setting Cursor Position of UITextField and UITextView in Swift

  3. 3

    Is it possible to get the "coordinates" of an element based on it's index?

  4. 4

    How to get a specific character from index of a string in swift

  5. 5

    Get index position of where vector changes value

  6. 6

    Index position of an element

  7. 7

    Getting value from string by index

  8. 8

    Python Pandas value from index position

  9. 9

    Get a value at index from range

  10. 10

    Get value from gson with index

  11. 11

    Get word/character screen position by its string index in Kinetic.js Text element

  12. 12

    RichTextBox get string from Index

  13. 13

    Swift: Search for String inside String and get Index

  14. 14

    Get key from HashMap in Android by position or index

  15. 15

    How to get the index of Differences in String from index - to index?

  16. 16

    Is it possible to get value of an index with ArrayList<Comparable>?

  17. 17

    How to get the value of a Range given an index in Swift

  18. 18

    Is it possible to get numeric index (position) of marks in tkinter text?

  19. 19

    Is it possible to get numeric index (position) of marks in tkinter text?

  20. 20

    Pop value from array by index of clicked element

  21. 21

    How to get String with specific index - swift

  22. 22

    Delete array element if index position is greater than a specific value

  23. 23

    Is it thread safe to get an element by index from List

  24. 24

    Realm dotnet: get element by index from RealmResults

  25. 25

    How to get the index of element from body tag?

  26. 26

    Get index from different element group

  27. 27

    Get index of element from array on click Jquery

  28. 28

    Remove item from List<string[]> or get index of any item at any position

  29. 29

    How to get index, position number of element with certain class with jQuery

HotTag

Archive