Home > Enterprise >  In iOS, Cursor position is not able to update from the Started event using the "SelectedTextRan
In iOS, Cursor position is not able to update from the Started event using the "SelectedTextRan

Time:12-06

Description

In iOS, not able to update the cursor position manually from the started event using the "SelectedTextRange" property on focus. Please refer to the below sample and screenshot.

Steps to Reproduce

  1. Create a custom control.
  2. Create native control by inheriting from UITextField.
  3. Hook started the event in native.
  4. Use the below code snippet to update the cursor position from the started event. Code Snippet:
var positionToSet = this.GetPosition(this.BeginningOfDocument, 3);                      
if (positionToSet != null)                                                                
{                                                                                          
    this.SelectedTextRange = this.GetTextRange(positionToSet, positionToSet);               
}                                                                                
       (OR)
  1. Run the attached sample.
  2. Cursor should be positioned in the 3rd index (next to 3) while focusing on the control.

Expected Behavior

The cursor should be positioned at the third index (next to 3) as shown in the below screenshot. enter image description here

Actual Behavior

The cursor gets positioned at the last.
enter image description here

Sample: https://github.com/xamarin/Xamarin.Forms/files/7653944/CustomControl.zip

Note: Issue reproduced only on or above iOS device 14.4.

CodePudding user response:

Its necessary to queue the change so that it occurs after Started event returns:

CoreFoundation.DispatchQueue.MainQueue.DispatchAsync( () => {
    this.SelectedTextRange = this.GetTextRange(positionToSet, positionToSet);
});

Based on Leo Dabus' answer in swift.

Tested on iOS simulator, iPhone 11, iOS 15.0.

  • Related