Forum Xamarin Xamarin.Forms

Load markdown text into the Xamarin.Forms Label

Selvamz16Selvamz16 Member ✭✭

Recently, Xamarin.Forms Label has support for FormattedText. So I want to use that FormattedText property to load the markdown text. For example,

I have below kinds of markdown text,

 1. **Hi**, How are you?
 2. Hello **John**, _Good Morning_

I want to automatically convert the markdown text like above into the FormattedString to set to the Label.FormattedText.

Could anyone help me to achieve this?

Note: I don't want to go for third party MarkdownView controls since they are heavy-weight control and having some issues on rendering the UI when I checked on Xamarin.Forms iOS.

Best Answer

  • Selvamz16Selvamz16 Member ✭✭
    edited June 26 Accepted Answer

    Finally, I wrote my own parsing and converted the markdown text into the FormattedString.

    public static FormattedString GetFormattedString(this string text, double defaultFontSize)
            {
                var boldFormat = "**";
                var italicFormat = "_";
                var formatString = new FormattedString();
                var temp = text;
                while(!string.IsNullOrWhiteSpace(temp))
                {
                    try
                    {
                        var boldIndex = temp.IndexOf(boldFormat);
                        var italicIndex = temp.IndexOf(italicFormat);
    
                        if (italicIndex >= 0 && (italicIndex < boldIndex || boldIndex < 0))
                        {
                            if (italicIndex > 0)
                            {
                                var t = temp.Substring(0, italicIndex);
                                formatString.Spans.Add(new Span() { Text = t });
                            }
                            temp = temp.Substring(italicIndex + 1);
                            var next = temp.IndexOf(italicFormat);
                            var t1 = temp.Substring(0, next);
                            formatString.Spans.Add(new Span() { Text = t1, FontAttributes = FontAttributes.Italic, FontSize = defaultFontSize });
                            temp = temp.Substring(next + 1);
                        }
                        else if (boldIndex >= 0)
                        {
                            if (boldIndex > 0)
                            {
                                var t = temp.Substring(0, boldIndex);
                                formatString.Spans.Add(new Span() { Text = t });
                            }
                            temp = temp.Substring(boldIndex + 2);
                            var next = temp.IndexOf(boldFormat);
                            var t1 = temp.Substring(0, next);
                            formatString.Spans.Add(new Span() { Text = t1, FontAttributes = FontAttributes.Bold, FontSize = defaultFontSize });
                            temp = temp.Substring(next + 2);
                        }
                        else
                        {
                            formatString.Spans.Add(new Span() { Text = temp, FontSize = defaultFontSize });
                            break;
                        }
                    }
                    catch (Exception)
                    {
                        formatString = new FormattedString();
                        formatString.Spans.Add(new Span() { Text = text, FontSize = defaultFontSize });
                        break;
                    }
                }
                return formatString;
            }
    

    Note: Currently, I have added code bold and italic formats only. Need to extend it for the required markdown formats.

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Parsing text isn't at all Xamarin. Its just C#.
    Have you tried any of the numerous text parsing article out there?
    https://www.google.com/search?q=C#+parse+text&amp;ie=utf-8&amp;oe=utf-8&amp;client=firefox-b-1-ab
    Parse through, add spans.

  • Selvamz16Selvamz16 Member ✭✭
    edited June 26 Accepted Answer

    Finally, I wrote my own parsing and converted the markdown text into the FormattedString.

    public static FormattedString GetFormattedString(this string text, double defaultFontSize)
            {
                var boldFormat = "**";
                var italicFormat = "_";
                var formatString = new FormattedString();
                var temp = text;
                while(!string.IsNullOrWhiteSpace(temp))
                {
                    try
                    {
                        var boldIndex = temp.IndexOf(boldFormat);
                        var italicIndex = temp.IndexOf(italicFormat);
    
                        if (italicIndex >= 0 && (italicIndex < boldIndex || boldIndex < 0))
                        {
                            if (italicIndex > 0)
                            {
                                var t = temp.Substring(0, italicIndex);
                                formatString.Spans.Add(new Span() { Text = t });
                            }
                            temp = temp.Substring(italicIndex + 1);
                            var next = temp.IndexOf(italicFormat);
                            var t1 = temp.Substring(0, next);
                            formatString.Spans.Add(new Span() { Text = t1, FontAttributes = FontAttributes.Italic, FontSize = defaultFontSize });
                            temp = temp.Substring(next + 1);
                        }
                        else if (boldIndex >= 0)
                        {
                            if (boldIndex > 0)
                            {
                                var t = temp.Substring(0, boldIndex);
                                formatString.Spans.Add(new Span() { Text = t });
                            }
                            temp = temp.Substring(boldIndex + 2);
                            var next = temp.IndexOf(boldFormat);
                            var t1 = temp.Substring(0, next);
                            formatString.Spans.Add(new Span() { Text = t1, FontAttributes = FontAttributes.Bold, FontSize = defaultFontSize });
                            temp = temp.Substring(next + 2);
                        }
                        else
                        {
                            formatString.Spans.Add(new Span() { Text = temp, FontSize = defaultFontSize });
                            break;
                        }
                    }
                    catch (Exception)
                    {
                        formatString = new FormattedString();
                        formatString.Spans.Add(new Span() { Text = text, FontSize = defaultFontSize });
                        break;
                    }
                }
                return formatString;
            }
    

    Note: Currently, I have added code bold and italic formats only. Need to extend it for the required markdown formats.

  • LandLuLandLu Member, Xamarin Team Xamurai

    @Selvamz16 This feature does not support markdown format. If you do want to show the strings with markdown format, you can try to use this third party library: https://github.com/aloisdeniel/MarkdownView.
    Use code behind to construct your markdown view:

    var mdView = new Xam.Forms.Markdown.MarkdownView();
    mdView.Markdown = "# Hello world\n\nThis is my first native markdown rendering";
    MyStack.Children.Add(mdView);
    

    More over you can install the version 0.6.1-pre, this adapt .NET Standard 2.0.

  • 1iveowl1iveowl USMember ✭✭
    edited September 2019
Sign In or Register to comment.