Ejemplo n.º 1
0
        internal static doc.Span RenderSpannableStringStyle(this SpannableStringStyle style)
        {
            doc.Span result;
            switch (style.Type)
            {
            case SpannableStringTypes.B:
            case SpannableStringTypes.Bold:
                result = new doc.Span {
                    FontWeight = FontWeights.Bold
                };
                break;

            case SpannableStringTypes.I:
            case SpannableStringTypes.Italic:
                result = new doc.Span {
                    FontStyle = FontStyle.Italic
                };
                break;

            case SpannableStringTypes.Font:
                var span = new doc.Span();
                foreach (var parameter in style.Parameters)
                {
                    switch (parameter.Key)
                    {
                    case SpannableStringParameterTypes.Size:
                        if (double.TryParse(parameter.Value, out double fontSize))
                        {
                            span.FontSize = fontSize;
                        }
                        break;

                    case SpannableStringParameterTypes.Color:
                        span.Foreground = new media.SolidColorBrush(Color.Parse(parameter.Value).Render());
                        break;

                    case SpannableStringParameterTypes.Face:
                        // TODO implement font faces.
                        break;

                    default: break;
                    }
                }

                result = span;
                break;

            default:
                result = new doc.Span();
                break;
            }

            if (style.InnerText.HasValue())
            {
                result.Inlines.Add(new doc.Run {
                    Text = style.InnerText
                });
            }
            return(result);
        }
        internal static void RenderSpannableStringStyle(this SpannableStringStyle style, TextView view, NSMutableAttributedString attributedString)
        {
            try
            {
                var range = new NSRange(style.Range.Start, style.Range.Length);
                switch (style.Type)
                {
                case SpannableStringTypes.B:
                case SpannableStringTypes.Bold:
                    attributedString.AddAttribute(UIStringAttributeKey.Font, UIFont.BoldSystemFontOfSize(view.Font.Size), range);
                    break;

                case SpannableStringTypes.I:
                case SpannableStringTypes.Italic:
                    attributedString.AddAttribute(UIStringAttributeKey.Font, UIFont.ItalicSystemFontOfSize(view.Font.Size), range);
                    break;

                case SpannableStringTypes.Font:
                    foreach (var parameter in style.Parameters)
                    {
                        switch (parameter.Key)
                        {
                        case SpannableStringParameterTypes.Size:
                            if (!float.TryParse(parameter.Value, out float fontSize))
                            {
                                break;
                            }
                            var font = UIFont.FromName(view.Font.Name, fontSize);
                            if (font == null)
                            {
                                break;
                            }
                            attributedString.AddAttribute(UIStringAttributeKey.Font, font, range);
                            break;

                        case SpannableStringParameterTypes.Color:
                            attributedString.AddAttribute(UIStringAttributeKey.ForegroundColor, Color.Parse(parameter.Value).Render(), range);
                            break;

                        // TODO implement font faces.
                        // case SpannableStringParameterTypes.Face: break;
                        default: break;
                        }
                    }
                    break;

                default: break;
                }
            }
            catch (Exception ex)
            {
                Log.For(typeof(IOSRenderExtentions)).Error(ex, $"Id: {view?.Id.Or("null")}, Text: {view?.Text.Or("null")}, Range: {style?.Range}");
            }
        }
        void RenderChildSpannableStyle(Android.Text.SpannableString text, SpannableStringStyle parentStyle)
        {
            foreach (var style in parentStyle.Children)
            {
                style.RenderSpannableStringStyle(text);

                if (style.Children.Count > 0)
                {
                    RenderChildSpannableStyle(text, style);
                }
            }
        }
Ejemplo n.º 4
0
        void RenderChildSpannableStyle(NSMutableAttributedString text, SpannableStringStyle parentStyle)
        {
            foreach (var style in parentStyle.Children)
            {
                style.RenderSpannableStringStyle(View, text);

                if (style.Children.Count > 0)
                {
                    RenderChildSpannableStyle(text, style);
                }
            }
        }
Ejemplo n.º 5
0
        void RenderChildSpannableStyle(Span parent, SpannableStringStyle parentStyle)
        {
            foreach (var style in parentStyle.Children)
            {
                var element = style.RenderSpannableStringStyle();
                if (style.Children.Count > 0)
                {
                    RenderChildSpannableStyle(element, style);
                }

                parent.Inlines.Add(element);
            }
        }