Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Xamarin Fomrs: Issue with text to speech feature for HTML data.

SreeeeSreeee INMember ✭✭✭✭✭

I am using the Label for showing HTML data on UI with TextType="Html" property. That feature works well and on UI HTML content gets converted to normal text.

I am also implemented text to speech features using Xamarin Essentials. When the TTS function starts I am highlighting the corresponding text using span property.

When the TTS function starts, the normal text gets converted into HTML data. How I fix this issue?

Screenshot:

enter image description here

I have uploaded a sample project here for the reference.

Best Answers

  • ColeXColeX Member, Xamarin Team Xamurai
    edited July 10 Accepted Answer

    We need to convert the html string first before setting span attribute , Regex could help .

    public static string GetHtmlText(string html)
    {
       html = System.Text.RegularExpressions.Regex.Replace(html, @"<\/*[^<>]*>", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
       html = html.Replace("\r\n", "").Replace("\r", "").Replace("&nbsp;", "").Replace(" ", "").Replace("\n\n\n", "\n");
        return html;
    }
    

    Usage

          //xxx
          string content = GetHtmlText(chapterDetails[0].htmlData);
         //xxx
    
  • ColeXColeX Member, Xamarin Team Xamurai
    Accepted Answer

    As joe said , format string only works with TextType Text , you should switch it back when clicking button .

                    label.FormattedText = formattedString;
                    label.TextType = TextType.Text;
                    await TextToSpeech.SpeakAsync(content);
    

Answers

  • JoeMankeJoeManke USMember ✭✭✭✭✭

    I'm going to hazard a guess that when you set the FormattedText property it wipes out the TextType value, thus turning your HTML into plain text.

  • ColeXColeX Member, Xamarin Team Xamurai
    edited July 10 Accepted Answer

    We need to convert the html string first before setting span attribute , Regex could help .

    public static string GetHtmlText(string html)
    {
       html = System.Text.RegularExpressions.Regex.Replace(html, @"<\/*[^<>]*>", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
       html = html.Replace("\r\n", "").Replace("\r", "").Replace("&nbsp;", "").Replace(" ", "").Replace("\n\n\n", "\n");
        return html;
    }
    

    Usage

          //xxx
          string content = GetHtmlText(chapterDetails[0].htmlData);
         //xxx
    
  • SreeeeSreeee INMember ✭✭✭✭✭

    @ColeX

    Working fine on android. On ios and windows platforms, when TTS starts the UI will be blank.

  • ColeXColeX Member, Xamarin Team Xamurai
    Accepted Answer

    As joe said , format string only works with TextType Text , you should switch it back when clicking button .

                    label.FormattedText = formattedString;
                    label.TextType = TextType.Text;
                    await TextToSpeech.SpeakAsync(content);
    
  • SreeeeSreeee INMember ✭✭✭✭✭

    @ColeX UI blank issue gets resolved after adding TextType.Text; But in windows, the text highlight is not working. For android and ios, space between the words is not added properly.

    Screenshot:

Sign In or Register to comment.