public void PlatformSizeIsModifiedByFontSizeFactor() { AssetPlugin plugin = new TestAssetPlugin(); AssetPlugin.GetPlatformFontSize(10.0f).Should().Be(10.0f); AssetPlugin.FontSizeFactor = 2.0f; AssetPlugin.GetPlatformFontSize(10.0f).Should().Be(20.0f); }
//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]); }
protected override void SetValueImpl(object target, object toSet) { var label = (TextView)target; Font font = toSet as Font; if (font != null) { try { Typeface droidFont = DroidAssetPlugin.GetCachedFont(font, label.Context); label.SetTypeface(droidFont, new TypefaceStyle()); float fontSize; if (font.Size != default(int)) { fontSize = AssetPlugin.GetPlatformFontSize(font.Size); label.SetTextSize(Android.Util.ComplexUnitType.Dip, fontSize); } else { fontSize = label.TextSize; } if (font.Color != System.Drawing.Color.Empty) { label.SetTextColor(font.Color.ToNativeColor()); } var multiplierFactor = AssetPlugin.LineHeightFactor ?? 1; var lineHeight = DroidAssetPlugin.GetPlatformLineHeight(font.Size, font.LineHeight * multiplierFactor); var lineSpacingMultiplier = font.LineHeightMultiplier.HasValue ? font.LineHeightMultiplier.Value : 1; label.SetLineSpacing(lineHeight, lineSpacingMultiplier); if (font.Alignment != TextAlignment.None) { label.Gravity = font.ToNativeAlignment(); } } catch { MvxBindingLog.Instance.Error("Failed to set font to Textview. Check if font exists, has a size and filename"); } } }
protected override void SetValueImpl(object target, object toSet) { var button = (Button)target; Font font = toSet as Font; if (font != null) { try { Typeface droidFont = DroidAssetPlugin.GetCachedFont(font, button.Context); button.SetTypeface(droidFont, new TypefaceStyle()); if (font.Size != default(int)) { button.SetTextSize(Android.Util.ComplexUnitType.Dip, AssetPlugin.GetPlatformFontSize(font.Size)); } if (font.Color != System.Drawing.Color.Empty) { List <int[]> states = new List <int[]>(); List <int> colors = new List <int>(); //if there's only a disabled color if (font.DisabledColor != System.Drawing.Color.Empty) { if (font.SelectedColor == null) { states.Add(new int[] { Android.Resource.Attribute.StateEnabled }); states.Add(new int[] { -Android.Resource.Attribute.StateEnabled }); } colors.Add(font.Color.ToArgb()); colors.Add(font.DisabledColor.ToArgb()); } if (font.SelectedColor != System.Drawing.Color.Empty) { if (font.DisabledColor == null) { states.Add(new int[] { Android.Resource.Attribute.StateActivated }); states.Add(new int[] { -Android.Resource.Attribute.StateActivated }); } colors.Add(font.SelectedColor.ToArgb()); colors.Add(font.Color.ToArgb()); } //if both disabled color and activated color are available if (font.DisabledColor != System.Drawing.Color.Empty && font.SelectedColor != System.Drawing.Color.Empty) { states.Add(new int[] { Android.Resource.Attribute.StateEnabled, -Android.Resource.Attribute.StateActivated }); states.Add(new int[] { -Android.Resource.Attribute.StateEnabled, -Android.Resource.Attribute.StateActivated }); states.Add(new int[] { Android.Resource.Attribute.StateEnabled, Android.Resource.Attribute.StateActivated }); states.Add(new int[] { -Android.Resource.Attribute.StateEnabled, Android.Resource.Attribute.StateActivated }); } if (states.Count > 0) { button.SetTextColor(new ColorStateList(states.ToArray(), colors.ToArray())); } else { button.SetTextColor(font.Color.ToNativeColor()); } } } catch { MvxBindingLog.Instance.Error("Failed to set font to Button. Check if font exists, has a size and filename"); } } }