summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core/Span.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Xamarin.Forms.Core/Span.cs')
-rw-r--r--Xamarin.Forms.Core/Span.cs169
1 files changed, 169 insertions, 0 deletions
diff --git a/Xamarin.Forms.Core/Span.cs b/Xamarin.Forms.Core/Span.cs
new file mode 100644
index 00000000..3789ddff
--- /dev/null
+++ b/Xamarin.Forms.Core/Span.cs
@@ -0,0 +1,169 @@
+using System;
+using System.ComponentModel;
+using System.Runtime.CompilerServices;
+
+namespace Xamarin.Forms
+{
+ [ContentProperty("Text")]
+ public sealed class Span : INotifyPropertyChanged, IFontElement
+ {
+ Color _backgroundColor;
+
+ Font _font;
+ FontAttributes _fontAttributes;
+ string _fontFamily;
+ double _fontSize;
+
+ Color _foregroundColor;
+ bool _inUpdate; // if we ever make this thread safe we need to move to a mutex
+
+ string _text;
+
+ public Span()
+ {
+ _fontFamily = null;
+ _fontAttributes = FontAttributes.None;
+ _fontSize = Device.GetNamedSize(NamedSize.Default, typeof(Label), true);
+ _font = Font.SystemFontOfSize(_fontSize);
+ }
+
+ public Color BackgroundColor
+ {
+ get { return _backgroundColor; }
+ set
+ {
+ if (_backgroundColor == value)
+ return;
+ _backgroundColor = value;
+ OnPropertyChanged();
+ }
+ }
+
+ [Obsolete("Please use the Font properties directly. Obsolete in 1.3.0")]
+ public Font Font
+ {
+ get { return _font; }
+ set
+ {
+ if (_font == value)
+ return;
+ _font = value;
+ OnPropertyChanged();
+ UpdateFontPropertiesFromStruct();
+ }
+ }
+
+ public Color ForegroundColor
+ {
+ get { return _foregroundColor; }
+ set
+ {
+ if (_foregroundColor == value)
+ return;
+ _foregroundColor = value;
+ OnPropertyChanged();
+ }
+ }
+
+ public string Text
+ {
+ get { return _text; }
+ set
+ {
+ if (_text == value)
+ return;
+ _text = value;
+ OnPropertyChanged();
+ }
+ }
+
+ public FontAttributes FontAttributes
+ {
+ get { return _fontAttributes; }
+ set
+ {
+ if (_fontAttributes == value)
+ return;
+ _fontAttributes = value;
+ OnPropertyChanged();
+ UpdateStructFromFontProperties();
+ }
+ }
+
+ public string FontFamily
+ {
+ get { return _fontFamily; }
+ set
+ {
+ if (_fontFamily == value)
+ return;
+ _fontFamily = value;
+ OnPropertyChanged();
+ UpdateStructFromFontProperties();
+ }
+ }
+
+ [TypeConverter(typeof(FontSizeConverter))]
+ public double FontSize
+ {
+ get { return _fontSize; }
+ set
+ {
+ if (_fontSize == value)
+ return;
+ _fontSize = value;
+ OnPropertyChanged();
+ UpdateStructFromFontProperties();
+ }
+ }
+
+ public event PropertyChangedEventHandler PropertyChanged;
+
+ void OnPropertyChanged([CallerMemberName] string propertyName = null)
+ {
+ PropertyChangedEventHandler handler = PropertyChanged;
+ if (handler != null)
+ handler(this, new PropertyChangedEventArgs(propertyName));
+ }
+
+ void UpdateFontPropertiesFromStruct()
+ {
+ if (_inUpdate)
+ return;
+ _inUpdate = true;
+
+ if (Font == Font.Default)
+ {
+ FontSize = Device.GetNamedSize(NamedSize.Default, typeof(Label), true);
+ FontFamily = null;
+ FontAttributes = FontAttributes.None;
+ }
+ else
+ {
+ FontSize = Font.UseNamedSize ? Device.GetNamedSize(Font.NamedSize, typeof(Label), true) : Font.FontSize;
+ FontFamily = Font.FontFamily;
+ FontAttributes = Font.FontAttributes;
+ }
+
+ _inUpdate = false;
+ }
+
+ void UpdateStructFromFontProperties()
+ {
+ if (_inUpdate)
+ return;
+ _inUpdate = true;
+
+ if (FontFamily != null)
+ {
+ Font = Font.OfSize(FontFamily, FontSize).WithAttributes(FontAttributes);
+ }
+ else
+ {
+ Font = Font.SystemFontOfSize(FontSize).WithAttributes(FontAttributes);
+ }
+
+ _inUpdate = false;
+ }
+ }
+} \ No newline at end of file