public static System.Windows.Controls.TextBlock CreateControl(TextBlock textBlock, RenderContext context)
        {
            Marked marked = new Marked();

            marked.Options.Renderer = new AdaptiveXamlMarkdownRenderer();
            marked.Options.Mangle   = false;
            marked.Options.Sanitize = true;

            string text = RendererUtilities.ApplyTextFunctions(textBlock.Text);
            // uiTextBlock.Text = textBlock.Text;
            string       xaml         = $"<TextBlock  xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">{marked.Parse(text)}</TextBlock>";
            StringReader stringReader = new StringReader(xaml);

            XmlReader xmlReader   = XmlReader.Create(stringReader);
            var       uiTextBlock = (System.Windows.Controls.TextBlock)XamlReader.Load(xmlReader);

            uiTextBlock.Style = context.GetStyle($"Adaptive.{textBlock.Type}");

            uiTextBlock.FontFamily   = new FontFamily(context.Config.FontFamily);
            uiTextBlock.TextWrapping = TextWrapping.NoWrap;

            switch (textBlock.Weight)
            {
            case TextWeight.Bolder:
                uiTextBlock.FontWeight = FontWeight.FromOpenTypeWeight(700);
                break;

            case TextWeight.Lighter:
                uiTextBlock.FontWeight = FontWeight.FromOpenTypeWeight(300);
                break;

            case TextWeight.Default:
            default:
                uiTextBlock.FontWeight = FontWeight.FromOpenTypeWeight(400);
                break;
            }

            uiTextBlock.TextTrimming = TextTrimming.CharacterEllipsis;

            if (textBlock.HorizontalAlignment != HorizontalAlignment.Left)
            {
                System.Windows.HorizontalAlignment alignment;
                if (Enum.TryParse <System.Windows.HorizontalAlignment>(textBlock.HorizontalAlignment.ToString(), out alignment))
                {
                    uiTextBlock.HorizontalAlignment = alignment;
                }
            }

            if (textBlock.Wrap)
            {
                uiTextBlock.TextWrapping = TextWrapping.Wrap;
            }

            return(uiTextBlock);
        }
        public static Xamarin.Forms.TextBlock CreateControl(TextBlock textBlock, RenderContext context)
        {
            var uiTextBlock = new Xamarin.Forms.TextBlock();

            uiTextBlock.Text  = RendererUtilities.ApplyTextFunctions(textBlock.Text);
            uiTextBlock.Style = context.GetStyle("Adaptive.TextBlock");
            // TODO: confirm text trimming
            uiTextBlock.LineBreakMode = LineBreakMode.TailTruncation;

            switch (textBlock.HorizontalAlignment)
            {
            case HorizontalAlignment.Left:
                uiTextBlock.HorizontalTextAlignment = TextAlignment.Start;
                break;

            case HorizontalAlignment.Center:
                uiTextBlock.HorizontalTextAlignment = TextAlignment.Center;
                break;

            case HorizontalAlignment.Right:
                uiTextBlock.HorizontalTextAlignment = TextAlignment.End;
                break;
            }

            uiTextBlock.TextColor = context.Resources.TryGetValue <Color>($"Adaptive.{textBlock.Color}");

            if (textBlock.Weight == TextWeight.Bolder)
            {
                uiTextBlock.FontAttributes = FontAttributes.Bold;
            }

            if (textBlock.Wrap == true)
            {
                uiTextBlock.LineBreakMode = LineBreakMode.WordWrap;
            }

            return(uiTextBlock);
        }
        protected static HtmlTag TextBlockRender(TypedElement element, RenderContext context)
        {
            TextBlock textBlock = (TextBlock)element;

            int fontSize;

            switch (textBlock.Size)
            {
            case TextSize.Small:
                fontSize = context.Config.FontSizes.Small;
                break;

            case TextSize.Medium:
                fontSize = context.Config.FontSizes.Medium;
                break;

            case TextSize.Large:
                fontSize = context.Config.FontSizes.Large;
                break;

            case TextSize.ExtraLarge:
                fontSize = context.Config.FontSizes.ExtraLarge;
                break;

            case TextSize.Normal:
            default:
                fontSize = context.Config.FontSizes.Normal;
                break;
            }
            int weight = 400;

            switch (textBlock.Weight)
            {
            case TextWeight.Lighter:
                weight = 200;
                break;

            case TextWeight.Bolder:
                weight = 600;
                break;
            }
            var lineHeight = fontSize * 1.2;

            var uiTextBlock = new DivTag()
                              .AddClass($"ac-{element.Type.Replace(".", "").ToLower()}")
                              .Style("text-align", textBlock.HorizontalAlignment.ToString().ToLower())
                              .Style("box-sizing", "border-box")
                              .Style("color", context.GetColor(textBlock.Color, textBlock.IsSubtle))
                              .Style("line-height", $"{lineHeight.ToString("F")}px")
                              .Style("font-size", $"{fontSize}px")
                              .Style("font-weight", $"{weight}");

            if (!String.IsNullOrEmpty(context.Config.FontFamily))
            {
                uiTextBlock = uiTextBlock
                              .Style("font-family", context.Config.FontFamily);
            }

            if (textBlock.MaxLines > 0)
            {
                uiTextBlock = uiTextBlock
                              .Style("max-height", $"{lineHeight * textBlock.MaxLines}px")
                              .Style("overflow", "hidden");
            }

            var setWrapStyleOnParagraph = false;

            if (textBlock.Wrap == false)
            {
                uiTextBlock = uiTextBlock
                              .Style("white-space", "nowrap");
                setWrapStyleOnParagraph = true;
            }
            else
            {
                uiTextBlock = uiTextBlock
                              .Style("word-wrap", "break-word");
            }

            var textTags = MarkdownToHtmlTagConverter.Convert(RendererUtilities.ApplyTextFunctions(textBlock.Text));

            uiTextBlock.Children.AddRange(textTags);

            Action <HtmlTag> setParagraphStyles = null;

            setParagraphStyles = (HtmlTag htmlTag) =>
            {
                if (htmlTag.Element?.ToLowerInvariant() == "p")
                {
                    htmlTag.Style("margin-top", "0px");
                    htmlTag.Style("margin-bottom", "0px");
                    htmlTag.Style("width", "100%");

                    if (setWrapStyleOnParagraph)
                    {
                        htmlTag.Style("text-overflow", "ellipsis");
                        htmlTag.Style("overflow", "hidden");
                    }
                }

                foreach (var child in htmlTag.Children)
                {
                    setParagraphStyles(child);
                }
            };

            setParagraphStyles(uiTextBlock);

            return(uiTextBlock);
        }
示例#4
0
        protected static HtmlTag TextBlockRender(TypedElement element, RenderContext context)
        {
            TextBlock textBlock = (TextBlock)element;

            int fontSize;

            switch (textBlock.Size)
            {
            case TextSize.Small:
                fontSize = context.Config.FontSizes.Small;
                break;

            case TextSize.Medium:
                fontSize = context.Config.FontSizes.Medium;
                break;

            case TextSize.Large:
                fontSize = context.Config.FontSizes.Large;
                break;

            case TextSize.ExtraLarge:
                fontSize = context.Config.FontSizes.ExtraLarge;
                break;

            case TextSize.Normal:
            default:
                fontSize = context.Config.FontSizes.Normal;
                break;
            }
            int weight = 400;

            switch (textBlock.Weight)
            {
            case TextWeight.Lighter:
                weight = 200;
                break;

            case TextWeight.Bolder:
                weight = 600;
                break;
            }
            var lineHeight = fontSize * 1.2;

            var uiTextBlock = new DivTag()
                              .AddClass($"ac-{element.Type.Replace(".", "").ToLower()}")
                              .Style("text-align", textBlock.HorizontalAlignment.ToString().ToLower())
                              .Style("box-sizing", "border-box")
                              .Style("color", context.GetColor(textBlock.Color, textBlock.IsSubtle))
                              .Style("line-height", $"{lineHeight.ToString("F")}px")
                              .Style("font-size", $"{fontSize}px")
                              .Style("font-weight", $"{weight}");

            if (!String.IsNullOrEmpty(context.Config.FontFamily))
            {
                uiTextBlock = uiTextBlock
                              .Style("font-family", context.Config.FontFamily);
            }

            if (textBlock.MaxLines > 0)
            {
                uiTextBlock = uiTextBlock
                              .Style("max-height", $"{lineHeight * textBlock.MaxLines}px")
                              .Style("overflow", "hidden");
            }

            var wrapStyle = "";

            if (textBlock.Wrap == false)
            {
                uiTextBlock = uiTextBlock
                              .Style("white-space", "nowrap");
                wrapStyle = "text-overflow: ellipsis; overflow: hidden";
            }
            else
            {
                uiTextBlock = uiTextBlock
                              .Style("word-wrap", "break-word");
            }

            var marked = new Marked();

            marked.Options.Mangle   = false;
            marked.Options.Sanitize = true;

            var html = marked.Parse(RendererUtilities.ApplyTextFunctions(textBlock.Text))
                       .Replace("<p>", $"<p style='margin-top: 0px;margin-bottom: 0px;width: 100%{wrapStyle}'>");

            uiTextBlock.Children.Add(new LiteralTag(html));

            return(uiTextBlock);
        }