locked
Retrieving text position from tap coordinates and coloring lines

    Question

  • Hi there

    In my App there is a text displayed. The user should be able to tap on a line and this line should then be highlighted. With RichTextBlock I can get a TextPointer from a tap position, but I am not able to set a background color for highlighting. Using a RichEditBox I can color the background (using rtf encoding) but I need to select text in order to react on a tap. Is there a more elegant solution to my problem? Actually I'd prefer not to use rtf, as this complicates the whole app by introducing the need to transform the text to rtf. I'm targeting for a Windows 8.1 App.

    Thanks for any suggestion! Chris

    Wednesday, December 3, 2014 12:39 AM

Answers

  • Found a solution using a RichEditBox. Its Document property features the GetRangeFromPoint method. Then expand the range to a line in order to set then its CharacterFormats BackgroundColor property. The code to handle the Tapped event is pretty short:

    void DiffText_Tapped(object sender, TappedRoutedEventArgs e)
    {
        ITextRange range = richEditBox.Document.GetRangeFromPoint(e.GetPosition(richEditBox), PointOptions.ClientCoordinates);
        range.Expand(TextRangeUnit.Line);
        range.CharacterFormat.BackgroundColor = Colors.Blue;
    }
    

    Note: setting the tapped event handler in XAML does not work, as the RichEditBox itself already consumes this event. Set the event handler programmatically in order to get the event:

    richEditBox.AddHandler(UIElement.TappedEvent, (TappedEventHandler)DiffText_Tapped, true);

    Sorry for the noise here...

    Chris

    Wednesday, December 3, 2014 6:24 AM

All replies

  • Found a solution using a RichEditBox. Its Document property features the GetRangeFromPoint method. Then expand the range to a line in order to set then its CharacterFormats BackgroundColor property. The code to handle the Tapped event is pretty short:

    void DiffText_Tapped(object sender, TappedRoutedEventArgs e)
    {
        ITextRange range = richEditBox.Document.GetRangeFromPoint(e.GetPosition(richEditBox), PointOptions.ClientCoordinates);
        range.Expand(TextRangeUnit.Line);
        range.CharacterFormat.BackgroundColor = Colors.Blue;
    }
    

    Note: setting the tapped event handler in XAML does not work, as the RichEditBox itself already consumes this event. Set the event handler programmatically in order to get the event:

    richEditBox.AddHandler(UIElement.TappedEvent, (TappedEventHandler)DiffText_Tapped, true);

    Sorry for the noise here...

    Chris

    Wednesday, December 3, 2014 6:24 AM
  • Nice to see the solution :)

    Should help other communities who meet the same problem.

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, December 4, 2014 2:23 AM
    Moderator