Created
April 18, 2017 20:01
-
-
Save ChaseFlorell/2840197f5dd63c7fad95a80bb3f577bb to your computer and use it in GitHub Desktop.
Build a formatted string for Xamarin Forms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using Xamarin.Forms; | |
namespace Helpers | |
{ | |
public class FontData | |
{ | |
public double FontSize { get; set; } | |
public FontAttributes FontAttributes { get; set; } | |
public Color TextColor { get; set; } | |
public string FontFamily { get; set; } | |
public static FontData DefaultValues() | |
{ | |
return new FontData | |
{ | |
FontSize = (double)Label.FontSizeProperty.DefaultValue, | |
FontAttributes = (FontAttributes)Label.FontAttributesProperty.DefaultValue, | |
TextColor = (Color)Label.TextColorProperty.DefaultValue, | |
FontFamily = Label.FontFamilyProperty.DefaultValue.ToString() | |
}; | |
} | |
public static FontData FromResource(string resourceName) | |
{ | |
var resource = Application.Current.Resources[resourceName]; | |
if (resource == null) | |
{ | |
return DefaultValues(); | |
} | |
var style = (Style) resource; | |
var data = new FontData(); | |
var colorSetter = style.Setters.FirstOrDefault(x => x.Property == Label.TextColorProperty); | |
var attrSetter= style.Setters.FirstOrDefault(x => x.Property == Label.FontAttributesProperty); | |
var fontSizeSetter= style.Setters.FirstOrDefault(x => x.Property == Label.FontSizeProperty); | |
var fontFamilySetter = style.Setters.FirstOrDefault(x => x.Property == Label.FontFamilyProperty); | |
data.TextColor = (Color?) colorSetter?.Value ?? (Color)Label.TextColorProperty.DefaultValue; | |
data.FontSize = (double?) fontSizeSetter?.Value ?? (double)Label.FontSizeProperty.DefaultValue; | |
data.FontFamily = fontFamilySetter != null && fontFamilySetter.Value != null | |
? fontFamilySetter.Value.ToString() | |
: Label.FontFamilyProperty.DefaultValue?.ToString(); | |
data.FontAttributes = attrSetter?.Value != null | |
? (FontAttributes) Enum.Parse(typeof (FontAttributes), attrSetter.Value.ToString()) | |
: (FontAttributes) Label.FontAttributesProperty.DefaultValue; | |
return data; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using Xamarin.Forms; | |
namespace Helpers | |
{ | |
public class FormattedStringBuilder | |
{ | |
private static readonly Dictionary<string, FontData> _fontDataCache = new Dictionary<string, FontData>(); | |
private readonly IList<Span> _spans = new List<Span>(); | |
private bool _withSpaces = true; | |
public FormattedStringBuilder Span(string text) | |
{ | |
return Span(text, styleResource: null); | |
} | |
public FormattedStringBuilder Span(string text, Span span) | |
{ | |
span.Text = text; | |
_spans.Add(span); | |
return this; | |
} | |
public FormattedStringBuilder Span(Span span) | |
{ | |
_spans.Add(span); | |
return this; | |
} | |
public FormattedStringBuilder Span(string text, string styleResource) | |
{ | |
if (string.IsNullOrEmpty(text)) | |
{ | |
throw new ArgumentNullException($"'Text' cannot be empty."); | |
} | |
FontData data; | |
if (_fontDataCache.ContainsKey(styleResource)) | |
{ | |
data = _fontDataCache[styleResource]; | |
} | |
else | |
{ | |
data = !string.IsNullOrWhiteSpace(styleResource) | |
? FontData.FromResource(styleResource) | |
: FontData.DefaultValues(); | |
_fontDataCache.Add(styleResource, data); | |
} | |
_spans.Add(new Span | |
{ | |
Text = text, | |
FontAttributes = data.FontAttributes, | |
FontFamily = data.FontFamily, | |
FontSize = data.FontSize, | |
ForegroundColor = data.TextColor | |
}); | |
return this; | |
} | |
public FormattedStringBuilder WithoutSpaces() | |
{ | |
_withSpaces = false; | |
return this; | |
} | |
public FormattedString Build() | |
{ | |
var result = new FormattedString(); | |
var count = _spans.Count; | |
for (var index = 0; index < count; index++) | |
{ | |
var span = _spans[index]; | |
result.Spans.Add(span); | |
if (index < count && _withSpaces) | |
{ | |
result.Spans.Add(new Span {Text = " "}); | |
} | |
} | |
return result; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment