Пример #1
0
        public IAssetPlugin AddFont(IBaseFont font, FontTag fontTag)
        {
            List <FontTag> tags = new List <FontTag>();

            tags.Add(fontTag);
            return(AddFont(font, tags));
        }
Пример #2
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);
        }
Пример #3
0
        public IAssetPlugin AddFont(IBaseFont font, List <FontTag> fontTags)
        {
            if (!CanAddFont(font))
            {
                return(this);
            }

            //convert the filename so the platform would understand this
            ConvertFontFileNameForPlatform(ref font);
            Fonts.Add(font.Name, font);

            //for each tag, add a font
            if (fontTags != null && fontTags.Count > 0)
            {
                if (FontsTagged != null)
                {
                    if (!FontsTagged.ContainsKey(font.Name))
                    {
                        FontsTagged[font.Name] = new List <FontTag>();
                    }
                    FontsTagged[font.Name].AddRange(fontTags);
                }
            }
            return(this);
        }
Пример #4
0
 public override bool CanAddFont(IBaseFont font)
 {
     if (font is iOSFont)
     {
         return(false);
     }
     return(base.CanAddFont(font));
 }
Пример #5
0
 public override void ConvertFontFileNameForPlatform(ref IBaseFont font)
 {
     //if the fontplatformname is already set, just use that one
     if (string.IsNullOrWhiteSpace(font.FontPlatformName))
     {
         font.FontPlatformName = RemoveFontExtension(font.FontFilename);
     }
 }
Пример #6
0
        public static Typeface GetCachedFont(IBaseFont font, Context context)
        {
            if (!_fontsCache.ContainsKey(font.Name))
            {
                //TODO make the 'Fonts' folder customizable for android
                _fontsCache[font.Name] = Typeface.CreateFromAsset(context.Assets, $"Fonts/{font.FontPlatformName}");
            }

            return(_fontsCache[font.Name]);
        }
Пример #7
0
        //TODO generic possible?
        public static UIFont GetCachedFont(IBaseFont font)
        {
            if (!_fontsCache.ContainsKey(font.Name))
            {
                if (font.Size == default(int))
                {
                    font.Size = 16;
                }
                _fontsCache[font.Name] = UIFont.FromName(font.FontPlatformName, AssetPlugin.GetPlatformFontSize(font.Size));
            }

            return(_fontsCache[font.Name]);
        }
Пример #8
0
        public virtual bool CanAddFont(IBaseFont font)
        {
            if (string.IsNullOrEmpty(font.Name))
            {
                throw new Exception("Added font should have a reference name");
            }

            if (string.IsNullOrEmpty(font.FontFilename))
            {
                throw new Exception("Added font should have a filename");
            }

            return(true);
        }
Пример #9
0
        public NSAttributedString ParseToAttributedText(string text, IBaseFont font)
        {
            try
            {
                if (font != null)
                {
                    this.ConvertFontFileNameForPlatform(ref font);

                    var assetPlugin = MvvmCross.Mvx.IoCProvider.Resolve <IAssetPlugin>();

                    string cleanText = string.Empty;

                    var indexPairs = AttributedFontHelper.GetFontTextBlocks(text, font.Name, assetPlugin, out cleanText);

                    NSMutableAttributedString attributedText = new NSMutableAttributedString(cleanText);

                    UIStringAttributes stringAttributes = CreateAttributesByFont(ref attributedText, font);
                    attributedText.AddAttributes(stringAttributes, new NSRange(0, cleanText.Length));

                    //TODO add caching for same fonttags for the attributes
                    foreach (FontIndexPair block in indexPairs)
                    {
                        //get the font for each tag and decorate the text
                        if (block.FontTag != null && !string.IsNullOrEmpty(block.FontTag.OriginalFontName))
                        {
                            FontTag fontTag = null;
                            var     tagFont = assetPlugin.GetFontByTagWithTag(font.Name, block.FontTag.Tag, out fontTag);

                            tagFont = tagFont == null ? font : tagFont;
                            UIStringAttributes attr = CreateAttributesByFont(ref attributedText, tagFont, block, fontTag);
                            attributedText.SetAttributes(attr, new NSRange(block.StartIndex, block.EndIndex - block.StartIndex));
                        }
                    }

                    return(attributedText);
                }
            }
            catch (Exception e)
            {
                //just return the text as passed if something fails
                return(new NSMutableAttributedString(text));
            }
            return(null);
        }
Пример #10
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;
        }
Пример #11
0
 public override void ConvertFontFileNameForPlatform(ref IBaseFont font)
 {
     //replace stripes, android doesn't like it
     font.FontPlatformName = font.FontFilename;
 }
Пример #12
0
 public override void ConvertFontFileNameForPlatform(ref IBaseFont font)
 {
     font.FontPlatformName = font.Name;
 }
Пример #13
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();
        }
Пример #14
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);
        }
Пример #15
0
 public abstract void ConvertFontFileNameForPlatform(ref IBaseFont font);
Пример #16
0
 public IAssetPlugin AddFont(IBaseFont font)
 {
     return(AddFont(font, new List <FontTag>()));
 }