AvaloniaUI / Avalonia

Develop Desktop, Embedded, Mobile and WebAssembly apps with C# and XAML. The most popular .NET UI client technology
https://avaloniaui.net
MIT License
26.1k stars 2.26k forks source link

GetCharacterIndexFromPoint method for TextBox #9540

Open rampaa opened 2 years ago

rampaa commented 2 years ago

Is your feature request related to a problem? Please describe. I want to get the index of the character from the TextBox that is closest to cursor position but I'm not sure if there's a straightforward way of achieving this.

Describe the solution you'd like WPF TextBox has a method called GetCharacterIndexFromPoint, a method equivalent to that would let me achieve my goal.

Gillibald commented 2 years ago

That should not be too complicated to add. The hard work is already done by the underlying TextLayout.

https://github.com/AvaloniaUI/Avalonia/blob/master/src/Avalonia.Base/Media/TextFormatting/TextLayout.cs#L283 for example

TextLayout is exposed to the TextBox via the _textPresenter

Contributions are welcome 🙏