Это будет ожидаемый эффект, потому что содержимое строки имеет формат html. В качестве обходного пути вы можете получить содержимое html, используя Regex .
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(" ", "").Replace(" ", "").Replace("\n\n\n", "\n");
return html;
}
Таким образом, вы можете улучшить код следующим образом:
public partial class MainPage : ContentPage
{
string[] strList;
public List<ChapterDetails> chapterDetails { get; set; }
public MainPage()
{
InitializeComponent();
//normal text
//string content = "Each platform supports different locales,\n to speak back text in different languages and accents.\n Platforms have different codes and ways of specifying the locale, \n which is why Xamarin provides a cross-platform Locale class and a way to query them with GetLocalesAsync.\n ";
//html text from epub file
chapterDetails = new List<ChapterDetails>();
string fileName = "Alices-Adventures-in-wonderland.epub";
var assembly = typeof(MainPage).GetTypeInfo().Assembly;
Stream stream = assembly.GetManifestResourceStream($"{assembly.GetName().Name}.{fileName}");
EpubBook epubBook = EpubReader.ReadBook(stream);
foreach (EpubChapter chapter in epubBook.Chapters)
{
chapterDetails.Add(new ChapterDetails() { title = chapter.Title, htmlData = chapter.HtmlContent, subChapters = chapter.SubChapters });
}
string content = GetHtmlText(chapterDetails[0].htmlData);
label.Text = content;
string str = ".";
char character = char.Parse(str);
string str2 = ",";
char character2 = char.Parse(str2);
string str3 = "\n";
char character3 = char.Parse(str3);
strList = content.Split(new char[] { character, character2 , character3});
}
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(" ", "").Replace(" ", "").Replace("\n\n\n", "\n");
return html;
}
private async void ClickedButton(object Sender, EventArgs args)
{
for (int i = 0; i < strList.Length; i++)
{
if(!string.IsNullOrEmpty(strList[i]))
{
string content = strList[i];
var formattedString = new FormattedString();
for (int j = 0; j < strList.Length; j++)
{
if (i == j)
{
formattedString.Spans.Add(new Span { Text = strList[j], ForegroundColor = Color.Black, BackgroundColor = Color.Gray });
}
else
{
formattedString.Spans.Add(new Span { Text = strList[j], ForegroundColor = Color.Black, });
}
}
label.FormattedText = formattedString;
label.TextType = TextType.Html;
await TextToSpeech.SpeakAsync(content);
}
}
}
}
Примечание: Таким образом, стиль css больше не будет работать. Вам необходимо самостоятельно установить стиль (шрифт или цвет) в span.