Getting Cursor Position of TextView in IOS



I am having a problem getting the current cursor position of a TextView element in iOS.

I have the following code:

const beginOfDoc = this.input.ios.beginningOfDocument;
const selectedRange = this.input.ios.selectedTextRange;

 // This errors
const cursorPos = this.input.ios.offset(beginOfDoc, selectedRange.start);

I have tried this as well:

const cursorPos = this.input.ios.offset({from: beginOfDoc, to: selectedRange.start});

but it errors all the same.

I am getting a problem though with the offset function. I got this far from this SO post:

Has anyone tried to get and successfully got the cursor position in iOS?

Thank you in advance for any insights you may have.


I figured it out with help of the tns-platform-declarations package.

const iosEle: UITextField = this.input.ios;
const beginOfDoc = iosEle.beginningOfDocument;
const selectedRange = iosEle.selectedTextRange;
const cursorPos = iosEle.offsetFromPositionToPosition(beginOfDoc, selectedRange.start);

Where am I in text field?

This is great and, inspired by this, I tried to work out how to SET the cursor position but failed, as most of the native methods that swift programmers seem to use (eg/ iosEle.setSelectedTextRange) appear to be unavailable within Nativescript?


NativeScript has no limitations on the apis you have in native iOS, you might be missing something. Try using tns-platform-declrations - it’s helpful, provides autocomplete feature for native apis if you are using TypeScript.


Yes! I have just been looking at Typescript, and will start converting my project to enable use of this!