Beispiel #1
0
 public void SetFont(Xamarin.Forms.Font f)
 {
     if (!equal(f, _font_xf))
     {
         _font_xf = f;
         _paints.Text.SetTypeface(f.ToTypeface());
         _paints.Text.TextSize = (float)f.FontSize;
         TextFontMetrics       = _paints.Text.GetFontMetrics();
     }
 }
Beispiel #2
0
        void UpdateToolbarTextFont(AppCompatTextView textView, Xamarin.Forms.Font customFont, Typeface originalFont)
        {
            if (customFont != null)
            {
                textView.Typeface = customFont.ToTypeface();

                float tValue = customFont.ToScaledPixel();
                textView.SetTextSize(ComplexUnitType.Sp, tValue);
            }
            else
            {
                textView.Typeface = originalFont;
            }
        }
Beispiel #3
0
        public static Typeface ToExtendedTypeface(this Xamarin.Forms.Font font, Context context)
        {
            Typeface typeface = null;

            //1. Lookup in the cache
            var hashKey = font.ToHasmapKey();

            typeface = TypefaceCache.SharedCache.RetrieveTypeface(hashKey);
#if DEBUG
            if (typeface != null)
            {
                Console.WriteLine("Typeface for font {0} found in cache", font);
            }
#endif

            //2. If not found, try custom asset folder
            if (typeface == null && !string.IsNullOrEmpty(font.FontFamily))
            {
                string filename = font.FontFamily;
                //if no extension given then assume and add .ttf
                if (filename.LastIndexOf(".", System.StringComparison.Ordinal) != filename.Length - 4)
                {
                    filename = string.Format("{0}.ttf", filename);
                }
                try
                {
                    var path = "fonts/" + filename;
#if DEBUG
                    Console.WriteLine("Lookking for font file: {0}", path);
#endif
                    typeface = Typeface.CreateFromAsset(context.Assets, path);
#if DEBUG
                    Console.WriteLine("Found in assets and cached.");
#endif
#pragma warning disable CS0168 // Variable is declared but never used
                }
                catch (Exception ex)
                {
#if DEBUG
                    Console.WriteLine("not found in assets. Exception: {0}", ex);
                    Console.WriteLine("Trying creation from file");
#endif
                    try
                    {
                        typeface = Typeface.CreateFromFile("fonts/" + filename);


#if DEBUG
                        Console.WriteLine("Found in file and cached.");
#endif
                    }
                    catch (Exception ex1)
#pragma warning restore CS0168 // Variable is declared but never used
                    {
#if DEBUG
                        Console.WriteLine("not found by file. Exception: {0}", ex1);
                        Console.WriteLine("Trying creation using Xamarin.Forms implementation");
#endif
                    }
                }
            }
            //3. If not found, fall back to default Xamarin.Forms implementation to load system font
            if (typeface == null)
            {
                typeface = font.ToTypeface();
            }

            if (typeface == null)
            {
#if DEBUG
                Console.WriteLine("Falling back to default typeface");
#endif
                typeface = Typeface.Default;
            }
            //Store in cache
            TypefaceCache.SharedCache.StoreTypeface(hashKey, typeface);

            return(typeface);
        }