Пример #1
0
        static string CleanseFontName(string fontName)
        {
            var fontFile = FontFile.FromString(fontName);

            if (!string.IsNullOrWhiteSpace(fontFile.Extension))
            {
                var(hasFont, _) = FontRegistrar.HasFont(fontFile.FileNameWithExtension());
                if (hasFont)
                {
                    return(fontFile.PostScriptName);
                }
            }
            else
            {
                foreach (var ext in FontFile.Extensions)
                {
                    var formated = fontFile.FileNameWithExtension(ext);
                    var(hasFont, filePath) = FontRegistrar.HasFont(formated);
                    if (hasFont)
                    {
                        return(fontFile.PostScriptName);
                    }
                }
            }
            return(fontFile.PostScriptName);
        }
Пример #2
0
        static (bool success, Typeface typeface) TryGetFromAssets(this string fontName)
        {
            //First check Alias
            var(hasFontAlias, fontPostScriptName) = FontRegistrar.HasFont(fontName);
            if (hasFontAlias)
            {
                return(true, Typeface.CreateFromFile(fontPostScriptName));
            }

            var isAssetFont = IsAssetFontFamily(fontName);

            if (isAssetFont)
            {
                return(LoadTypefaceFromAsset(fontName));
            }

            var folders = new[]
            {
                "",
                "Fonts/",
                "fonts/",
            };


            //copied text
            var fontFile = FontFile.FromString(fontName);

            if (!string.IsNullOrWhiteSpace(fontFile.Extension))
            {
                var(hasFont, fontPath) = FontRegistrar.HasFont(fontFile.FileNameWithExtension());
                if (hasFont)
                {
                    return(true, Typeface.CreateFromFile(fontPath));
                }
            }
            else
            {
                foreach (var ext in FontFile.Extensions)
                {
                    var formated = fontFile.FileNameWithExtension(ext);
                    var(hasFont, fontPath) = FontRegistrar.HasFont(formated);
                    if (hasFont)
                    {
                        return(true, Typeface.CreateFromFile(fontPath));
                    }

                    foreach (var folder in folders)
                    {
                        formated = $"{folder}{fontFile.FileNameWithExtension()}#{fontFile.PostScriptName}";
                        var result = LoadTypefaceFromAsset(formated);
                        if (result.success)
                        {
                            return(result);
                        }
                    }
                }
            }

            return(false, null);
        }
Пример #3
0
        private void SetFormattedText()
        {
            if (Control != null)
            {
                var text = new NSMutableAttributedString(string.Empty);
                foreach (var span in Element.FormattedText.Spans)
                {
                    var range   = new NSRange(0, span.Text.Length);
                    var subText = new NSMutableAttributedString(span.Text, foregroundColor: span.TextColor.ToUIColor());

                    var font = UIFont.SystemFontOfSize(new nfloat(span.FontSize));
                    if (string.IsNullOrEmpty(span.FontFamily))
                    {
                        switch (label.FontAttributes)
                        {
                        case FontAttributes.None:
                            font = UIFont.SystemFontOfSize(new nfloat(span.FontSize));
                            break;

                        case FontAttributes.Bold:
                            font = UIFont.BoldSystemFontOfSize(new nfloat(span.FontSize));
                            break;

                        case FontAttributes.Italic:
                            font = UIFont.ItalicSystemFontOfSize(new nfloat(span.FontSize));
                            break;

                        default:
                            font = UIFont.BoldSystemFontOfSize(new nfloat(span.FontSize));
                            break;
                        }
                    }
                    else
                    {
                        var exportFonts = FontRegistrar.HasFont(span.FontFamily);
                        if (exportFonts.hasFont)
                        {
                            var pathName = exportFonts.fontPath;
                            pathName = pathName.Replace(".ttf", string.Empty).Replace(".otf", string.Empty);
                            font     = UIFont.FromName(pathName, new nfloat(span.FontSize));
                        }
                        else
                        {
                            var fn = GetFontName(span.FontFamily, span.FontAttributes);
                            font = UIFont.FromName(fn, new nfloat(span.FontSize));
                        }
                    }
                    subText.AddAttribute(UIStringAttributeKey.Font, font, range);

                    if (span.BackgroundColor != Color.Default)
                    {
                        subText.AddAttribute(UIStringAttributeKey.BackgroundColor, span.BackgroundColor.ToUIColor(), range);
                    }

                    text.Append(subText);
                }

                Control.AttributedText = text;
            }
        }
Пример #4
0
        private static string CleanseFontName(string fontName)
        {
            //First check Alias
            (bool hasFontAlias, string fontPostScriptName) = FontRegistrar.HasFont(fontName);
            if (hasFontAlias)
            {
                return(fontPostScriptName);
            }

            FontFile fontFile = FontFile.FromString(fontName);

            if (!string.IsNullOrWhiteSpace(fontFile.Extension))
            {
                (bool hasFont, string filePath) = FontRegistrar.HasFont(fontFile.FileNameWithExtension());
                if (hasFont)
                {
                    return(filePath ?? fontFile.PostScriptName);
                }
            }
            else
            {
                foreach (string ext in FontFile.Extensions)
                {
                    string formated = fontFile.FileNameWithExtension(ext);
                    (bool hasFont, string filePath) = FontRegistrar.HasFont(formated);
                    if (hasFont)
                    {
                        return(filePath);
                    }
                }
            }

            return(fontFile.PostScriptName);
        }
Пример #5
0
        static IEnumerable <string> GetAllFontPossibilities(string fontFamily)
        {
            const string path = "Assets/Fonts/";

            string[] extensions = new[]
            {
                ".ttf",
                ".otf",
            };

            var fontFile = FontFile.FromString(fontFamily);
            //If the extension is provided, they know what they want!
            var hasExtension = !string.IsNullOrWhiteSpace(fontFile.Extension);

            if (hasExtension)
            {
                var(hasFont, filePath) = FontRegistrar.HasFont(fontFile.FileNameWithExtension());
                if (hasFont)
                {
                    var formated = $"{filePath}#{fontFile.GetPostScriptNameWithSpaces()}";
                    yield return(formated);

                    yield break;
                }
                else
                {
                    yield return($"{path}{fontFile.FileNameWithExtension()}");
                }
            }
            foreach (var ext in extensions)
            {
                var(hasFont, filePath) = FontRegistrar.HasFont(fontFile.FileNameWithExtension(ext));
                if (hasFont)
                {
                    var formatted = $"{filePath}#{fontFile.GetPostScriptNameWithSpaces()}";
                    yield return(formatted);

                    yield break;
                }
            }

            //Always send the base back
            yield return(fontFamily);

            foreach (var ext in extensions)
            {
                var formatted = $"{path}{fontFile.FileNameWithExtension(ext)}#{fontFile.GetPostScriptNameWithSpaces()}";
                yield return(formatted);
            }
        }
Пример #6
0
        static (bool success, FontFamily fontFamily) TryGetFromAssets(this string fontName)
        {
            //First check Alias
            var(hasFontAlias, fontPostScriptName) = FontRegistrar.HasFont(fontName);
            if (hasFontAlias)
            {
                return(true, CreateFromFile(fontPostScriptName));
            }

            var folders = new[]
            {
                "",
                "Fonts/",
                "fonts/",
            };


            //copied text
            var fontFile = FontFile.FromString(fontName);

            if (!string.IsNullOrWhiteSpace(fontFile.Extension))
            {
                var(hasFont, fontPath) = FontRegistrar.HasFont(fontFile.FileNameWithExtension());
                if (hasFont)
                {
                    return(true, CreateFromFile(fontPath));
                }
            }
            else
            {
                foreach (var ext in FontFile.Extensions)
                {
                    var formated = fontFile.FileNameWithExtension(ext);
                    var(hasFont, fontPath) = FontRegistrar.HasFont(formated);
                    if (hasFont)
                    {
                        return(true, CreateFromFile(fontPath));
                    }
                }
            }

            return(false, null);
        }
Пример #7
0
        private void UpdateFormattedText()
        {
            if (Element?.FormattedText == null)
            {
                return;
            }

            var spanList = new List <SpannableString>();

            foreach (var span in label.FormattedText.Spans)
            {
                var ss = new SpannableString(span.Text);

                ss.SetSpan(new ForegroundColorSpan(span.TextColor.ToAndroid()), 0, span.Text.Length, SpanTypes.ExclusiveExclusive);
                ss.SetSpan(new AbsoluteSizeSpan((int)span.FontSize, true), 0, span.Text.Length, SpanTypes.ExclusiveExclusive);
                if (!string.IsNullOrEmpty(span.FontFamily))
                {
                    var exportFonts = FontRegistrar.HasFont(span.FontFamily);
                    if (exportFonts.hasFont)
                    {
                        var tf = Typeface.CreateFromFile(exportFonts.fontPath);
                        ss.SetSpan(new TypefaceSpan(tf), 0, span.Text.Length, SpanTypes.ExclusiveExclusive);
                    }
                    else
                    {
                        var tf = GetTypeface();
                        ss.SetSpan(new TypefaceSpan(span.FontFamily), 0, span.Text.Length, SpanTypes.ExclusiveExclusive);
                    }
                }
                else
                {
                    var tf = GetTypeface();
                    ss.SetSpan(new TypefaceSpan(span.FontFamily), 0, span.Text.Length, SpanTypes.ExclusiveExclusive);
                }

                spanList.Add(ss);
            }

            var result = new SpannableString(TextUtils.ConcatFormatted(spanList.ToArray()));

            Control.TextFormatted = result;
        }