Ejemplo n.º 1
0
        private void SetFont(ref SpannableString converted, IBaseFont font, FontIndexPair pair, FontTag fontTag)
        {
            //set the text color
            if (font.Color != System.Drawing.Color.Empty)
            {
                converted.SetSpan(new ForegroundColorSpan(font.Color.ToNativeColor()), pair.StartIndex, pair.EndIndex, SpanTypes.ExclusiveInclusive);
            }

            if (fontTag != null && fontTag.FontAction == FontTagAction.Link)
            {
                CreateLink(ref converted, font, pair);
            }

            //set allignment
            if (font is Font)
            {
                Font taggedExtendedFont = font as Font;

                if (taggedExtendedFont.Alignment != TextAlignment.None)
                {
                    Layout.Alignment alignment = taggedExtendedFont.Alignment == TextAlignment.Center ? Layout.Alignment.AlignCenter : Layout.Alignment.AlignNormal;
                    converted.SetSpan(new AlignmentSpanStandard(alignment), pair.StartIndex, pair.EndIndex, SpanTypes.ExclusiveInclusive);
                }
            }

            if (_extendedFont != null)
            {
                //calculate the relative size to the regular font
                converted.SetSpan(new RelativeSizeSpan((float)font.Size / (float)_extendedFont.Size), pair.StartIndex, pair.EndIndex, SpanTypes.ExclusiveInclusive);
            }
            //set the custom typeface
            converted.SetSpan(new CustomTypefaceSpan("sans-serif", DroidAssetPlugin.GetCachedFont(font, _context)), pair.StartIndex, pair.EndIndex, SpanTypes.ExclusiveInclusive);
        }
Ejemplo n.º 2
0
        private void CreateLink(ref SpannableString converted, IBaseFont font, FontIndexPair pair)
        {
            //if theres a link property, use that one, if not, use the text itself
            string link;

            if (pair.TagProperties != null && pair.TagProperties.ContainsKey("href"))
            {
                link = pair.TagProperties.GetValueOrDefault("href");
            }
            else
            {
                link = converted.ToString().Substring(pair.StartIndex, pair.EndIndex - pair.StartIndex).Trim();
            }
            converted.SetSpan(new ClickableLinkSpan()
            {
                Link = link
            }, pair.StartIndex, pair.EndIndex, SpanTypes.ExclusiveInclusive);
            _clickableFont = font;
            _containsLink  = true;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Set the attributes of a part of the text
        /// </summary>
        /// <param name="converted">Converted.</param>
        /// <param name="pair">Pair.</param>
        private void SetAttributed(SpannableString converted, FontIndexPair pair, Font fallbackFont)
        {
            //get the font by tags

            FontTag fontTag = null;

            if (pair.FontTag != null)
            {
                var taggedFont = _assetPlugin.GetFontByTagWithTag(fallbackFont.Name, pair.FontTag.Tag, out fontTag);

                if (taggedFont != null)
                {
                    SetFont(ref converted, taggedFont, pair, fontTag);
                    return;
                }
            }

            if (fallbackFont != null)
            {
                SetFont(ref converted, fallbackFont, pair, fontTag);
            }
        }
Ejemplo n.º 4
0
        private void CreateLink(ref NSMutableAttributedString text, ref UIStringAttributes attribute, IBaseFont font, FontIndexPair pair)
        {
            //if theres a link property, use that one, if not, use the text itself
            string link;

            if (pair.TagProperties != null && pair.TagProperties.ContainsKey("href"))
            {
                link = pair.TagProperties.GetValueOrDefault("href");
            }
            else
            {
                link = text.Value.Substring(pair.StartIndex, pair.EndIndex - pair.StartIndex).Trim();
            }
            try
            {
                attribute.Link = new NSUrl(link);
            }
            catch (Exception e)
            {
                MvxBindingLog.Instance.Error($"Cannot convert {link} to url", e.ToLongString());
            }
            attribute.UnderlineStyle = NSUnderlineStyle.Single;
            attribute.UnderlineColor = font.Color.ToNativeColor();
        }
Ejemplo n.º 5
0
        private UIStringAttributes CreateAttributesByFont(ref NSMutableAttributedString text, IBaseFont font, FontIndexPair pair = null, FontTag tag = null)
        {
            UIStringAttributes stringAttributes = new UIStringAttributes {
            };

            //add the font
            stringAttributes.Font = TouchAssetPlugin.GetCachedFont(font);

            //add the color
            if (font.Color != System.Drawing.Color.Empty)
            {
                stringAttributes.ForegroundColor = font.Color.ToNativeColor();
            }

            if (pair != null && tag != null)
            {
                if (tag.FontAction == FontTagAction.Link)
                {
                    CreateLink(ref text, ref stringAttributes, font, pair);
                }
            }

            if (font is Font)
            {
                var extendedFont = font as Font;

                if (stringAttributes.ParagraphStyle == null)
                {
                    stringAttributes.ParagraphStyle = new NSMutableParagraphStyle();
                }

                if (extendedFont.Alignment != TextAlignment.None)
                {
                    UITextAlignment alignment = extendedFont.ToNativeAlignment();
                    stringAttributes.ParagraphStyle.Alignment = alignment;
                }

                //add the lineheight
                stringAttributes.ParagraphStyle.LineSpacing = GetPlatformLineHeight(font.Size, extendedFont.LineHeight);

                stringAttributes.ParagraphStyle.LineBreakMode = extendedFont.ToNativeLineBreakMode();

                stringAttributes.ParagraphStyle.LineHeightMultiple = extendedFont.LineHeightMultiplier.HasValue ? (float)extendedFont.LineHeightMultiplier.Value : 0f;
            }

            return(stringAttributes);
        }