Moving cursor position by offset value - perl

$text->SetCursor( position );
This is used to move cursor at the given position.
Is there any way that I can move the cursor by a given offset value?
Thanks,

You just have to use the correct position descriptor; they support a little language of their own. For example, the string insert + 1 char describes the location one character further on than the insertion point (insert is a special kind of mark that represents where text will be inserted and is where the caret is located when focus is in the text widget).

Related

TextField's current focus position in flutter

Hey I have a TextField and a controller for it through which I access its current text value and add some text like (Some Special Asterix or text with angular brackets just like hashnode's text editor) to it when needed, adding text at the end is easy.
controller.text += "Something";
The above code will add Something to the end. But I need to know the current TextFields Cursor position and add text according to it.
I really love to know how we can do it in flutter efficiently
Try this to get the cursor position
print(controller.selection.baseOffset);
print(controller.selection.extentOffset);
if they are the same, it is current cursor position.
if not, it means that some of text is selected, baseOffset is start position and extentOffset is end positon.

What is TextAffinity in Flutter?

The TextAffinity enum in Flutter has the following documentation description:
A way to disambiguate a TextPosition when its offset could match two
different locations in the rendered string.
For example, at an offset where the rendered text wraps, there are two
visual positions that the offset could represent: one prior to the
line break (at the end of the first line) and one after the line break
(at the start of the second line). A text affinity disambiguates
between these two cases.
This affects only line breaks caused by wrapping, not explicit newline
characters. For newline characters, the position is fully specified by
the offset alone, and there is no ambiguity.
TextAffinity also affects bidirectional text at the interface between
LTR and RTL text. Consider the following string, where the lowercase
letters will be displayed as LTR and the uppercase letters RTL:
"helloHELLO". When rendered, the string would appear visually as
"helloOLLEH". An offset of 5 would be ambiguous without a
corresponding TextAffinity. Looking at the string in code, the offset
represents the position just after the "o" and just before the "H".
When rendered, this offset could be either in the middle of the string
to the right of the "o" or at the end of the string to the right of
the "H".
This kind of makes sense but it's still a little confusing. For example "helloHELLO" isn't actually bidirectional text. How is offset 5 ambiguous? Do you have a visual example?
I'm adding my answer below.
TextAffinity disambiguates text position for soft wraps and bidirectional text boundaries. The affinity can be either upstream or downstream. Downstream is the default.
Soft wrap position
Given the string 0123456789 whose last character soft-wraps to the next line, you can choose the cursor position as follows. Both positions are at offset 9:
Downstream
Upstream
Bidirectional text boundary
Given the string Helloשלום where Hello is LRT text and שלום is RTL text, you can choose the cursor position at the boundary between them as follows. Both positions are at offset 5:
Downstream
Upstream
Going on
I write about this in more detail here.

read text at cursor position in JFace TextViewer

I have an editor that extends a TextEditor class. I would like to know if it is possible to read the text at the cursor position within the editor.
Using ITextSelection is not useful since it expects that some text string is selected. I require to read text when the cursor is at some point and no text is selected.
Thanks.
ITextViewer.getSelectedRange(). It will return the offset and the size of the selection. Offset is the caret position and size may be 0. To read the text, use ITextViewer.getDocument().get() and analyze the contents at the respective offset.

How to figure out the current caret position in a UITextField?

I want to programmatically paste a string into a text field or text view at the current caret position.
Is there an easy way to do this? I would need to know the current caret position, but there's no method to retrieve it, right? Don't want to call private API. Is there any legal way to do it?
Use UITextView's selectedRange method. If the range has a non-zero length (in the case of the user having selected some text), just take the location (or NSMaxRange(), or replace the entire range...)
Instead of pasting option. Get the string from textfield or textview insert the string which you want to paste it by providing the index and then clear the textfield then pass the string which you inserted.

move cursor up/down in UITextView

The only public API I found allowed me only to move the cursor right/left using selectedRange property.
Any workaround to perform that task up/down?
Thanks
If you haven't found an answer, you may just have to develop your own custom component using Core Text.
This is a pretty old question ... but for anyone still trying to find a way to do this:
You can count the number of \n characters up to the current range.location and set the new range.location to a \n before to the cursor up a row or to a \n after the cursor to move it down.
If you want to move it up or down and keep it in line with the characters on the row you will need to count the number of characters on each row and move it to the right location