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); }
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"); } } }