Forum Xamarin.Forms

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Webview selection position

snowlsnowl Member ✭✭
edited October 2020 in Xamarin.Forms
I have my own web page content and I use webview to show it.I want to select a part of text and highlight it.
I should be able store the page so if I insert <mark> tag to selected text I can show up highlight and I can store the page.
How can I get the position of selection both ios and android ?
Is there an easy way with JavaScript ?
Tagged:

Answers

  • LucasZhangLucasZhang Member, Xamarin Team Xamurai

    Since you can store the web page , you could get the content of the whole string and the selection string . Then use the following code to get the index .

    string ContentStr = "xxxxxxx";
    string SelectionStr = "xxx";
    
    int index = ContentStr .IndexOf(SelectionStr);
    
  • snowlsnowl Member ✭✭
    edited October 2020

    @LucasZhang said:
    Since you can store the web page , you could get the content of the whole string and the selection string . Then use the following code to get the index .

    string ContentStr = "xxxxxxx";
    string SelectionStr = "xxx";
    
    int index = ContentStr .IndexOf(SelectionStr);
    

    But there may be more than one same substring.
    For example: This is a string.This is a apple.
    Selected text : second is
    index of finds the first is ?
    I should find the index of selected string

  • LucasZhangLucasZhang Member, Xamarin Team Xamurai
    public  List<int> AllIndexesOf(string ContentStr, string value)
            {
                List<int> indexes = new List<int>();
    
                if (!string.IsNullOrEmpty(value))
                {
                    for (int index = 0; ; index += value.Length)
                    {
                        index = ContentStr.IndexOf(value, index);
                        if (index == -1)
                            return indexes;
                        indexes.Add(index);
                    }
                }
    
                return indexes;
            }
    
  • snowlsnowl Member ✭✭

    @LucasZhang said:

    public  List<int> AllIndexesOf(string ContentStr, string value)
            {
                List<int> indexes = new List<int>();
    
                if (!string.IsNullOrEmpty(value))
                {
                    for (int index = 0; ; index += value.Length)
                    {
                        index = ContentStr.IndexOf(value, index);
                        if (index == -1)
                            return indexes;
                        indexes.Add(index);
                    }
                }
    
                return indexes;
            }
    

    Yes I can take all indexes like this.But this is not solution.Still we don't know which index is actual index

  • LucasZhangLucasZhang Member, Xamarin Team Xamurai

    If so you could handle the logic in JS

  • snowlsnowl Member ✭✭
    Can I use rangy in this app ? @LucasZhang
  • snowlsnowl Member ✭✭

    I achive this with using Rangy.
    How can I add menu item to long press menu ? @LucasZhang

Sign In or Register to comment.