private static bool equal(Xamarin.Forms.Font c1, Xamarin.Forms.Font c2) { if (c1 == c2) { return(true); } if (c1.UseNamedSize != c2.UseNamedSize) { return(false); } if (c1.UseNamedSize) { if (c1.NamedSize != c2.NamedSize) { return(false); } } else { if (c1.FontSize != c2.FontSize) { return(false); } } if (c1.FontFamily != c2.FontFamily) { return(false); } return(true); }
public static void ApplyFont(this TextElement self, Xamarin.Forms.Font font) { self.FontSize = font.UseNamedSize ? font.NamedSize.GetFontSize() : font.FontSize; self.FontFamily = FontService.GetWinFontFamily(font.FontFamily); self.FontStyle = font.FontAttributes.HasFlag(FontAttributes.Italic) ? Windows.UI.Text.FontStyle.Italic : Windows.UI.Text.FontStyle.Normal; self.FontWeight = font.FontAttributes.HasFlag(FontAttributes.Bold) ? FontWeights.Bold : FontWeights.Normal; }
public static CrossGraphics.Font ToCrossFont(this Xamarin.Forms.Font f) { var options = FontOptions.None; if (0 != (f.FontAttributes & FontAttributes.Bold)) { options = FontOptions.Bold; } return(new CrossGraphics.Font(f.FontFamily, options, (int)f.FontSize)); }
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(); } }
private void UpdateItemsSource() { var font = string.IsNullOrEmpty(Element.FontFamily) ? Font.SystemFontOfSize(Element.FontSize) : Font.OfSize(Element.FontFamily, Element.FontSize); var nativeFont = font.ToUIFont(); Control.Model = new MyDataModel(Element.ItemsSource, row => { Element.SelectedIndex = row; }, nativeFont); }
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; } }
public void SetFont(Xamarin.Forms.Font f) { _font_xf = f; // We have a Xamarin.Forms.Font object. We need a CTFont. X.F.Font has // ToUIFont() but not a Core Text version of same. So we make a UIFont first. _font_ui = _font_xf.ToUIFont(); _font_ct = new CTFont(_font_ui.Name, _font_ui.PointSize); // With Core Text, it is important that we use a CTStringAttributes() with the // NSAttributedString constructor. // TODO maybe we should have the text color be separate, have it be an arg on SetFont? _ct_attr = new CTStringAttributes { Font = _font_ct, ForegroundColorFromContext = true, //ForegroundColor = _clr_cg, // ParagraphStyle = new CTParagraphStyle(new CTParagraphStyleSettings() { Alignment = CTTextAlignment.Center }) }; }
protected virtual void Dispose(bool disposing) { Font?.Dispose(); BackgroundImage?.Dispose(); }
bool Equals(Font other) { return(string.Equals(FontFamily, other.FontFamily) && FontSize.Equals(other.FontSize) && NamedSize == other.NamedSize && FontAttributes == other.FontAttributes); }
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); }
private static string ToHasmapKey(this Xamarin.Forms.Font font) { return(string.Format("{0}.{1}.{2}.{3}", font.FontFamily, font.FontSize, font.NamedSize, (int)font.FontAttributes)); }
void IFontElement.OnFontChanged(Font oldValue, Font newValue) => InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
void IFontElement.OnFontChanged(Font oldValue, Font newValue) { }