Skip to content

Instantly share code, notes, and snippets.

@Clancey
Created July 18, 2019 00:10
Show Gist options
  • Save Clancey/62ed81202590e8582580a8be5e50a36d to your computer and use it in GitHub Desktop.
Save Clancey/62ed81202590e8582580a8be5e50a36d to your computer and use it in GitHub Desktop.
using System;
using Xamarin.Forms;
using Xamarin.Forms.Internals;
namespace gMusic.Views
{
public class OverflowLabel : ContentView, IFontElement
{
public static readonly BindableProperty HorizontalTextAlignmentProperty =
BindableProperty.Create(nameof(HorizontalTextAlignment), typeof(TextAlignment), typeof(OverflowLabel), TextAlignment.Start,
propertyChanged: (b, o, n) => (b as OverflowLabel)?.SetHorizontalTextAlignment((TextAlignment)n));
public static readonly BindableProperty TextProperty =
BindableProperty.Create(nameof(Text), typeof(string), typeof(OverflowLabel), null,
propertyChanged: (b, o, n) => (b as OverflowLabel)?.SetText((string)n));
public static readonly BindableProperty FontProperty =
BindableProperty.Create(nameof(Font), typeof(Font), typeof(OverflowLabel), default(Font),
propertyChanged: (b, o, n) => (b as OverflowLabel)?.SetFont((Font)n));
public static readonly BindableProperty FontFamilyProperty =
BindableProperty.Create(nameof(FontFamily), typeof(string), typeof(OverflowLabel), default(string),
propertyChanged: (b, o, n) => (b as OverflowLabel)?.SetFontFamily((string)n));
public static readonly BindableProperty FontSizeProperty =
BindableProperty.Create(nameof(FontSize), typeof(double), typeof(OverflowLabel), -1.0,
propertyChanged: (b, o, n) => (b as OverflowLabel)?.SetFontSize((double)n),
defaultValueCreator: FontSizeDefaultValueCreator);
public static readonly BindableProperty FontAttributesProperty =
BindableProperty.Create(nameof(FontAttributes), typeof(FontAttributes), typeof(OverflowLabel), FontAttributes.None,
propertyChanged: (b, o, n) => (b as OverflowLabel)?.SetFontAttributes((FontAttributes)n));
public static readonly BindableProperty TextColorProperty =
BindableProperty.Create(nameof(OverflowLabel.TextColor), typeof(Color), typeof(OverflowLabel), Color.Default,
propertyChanged: OnTextColorPropertyChanged);
static void OnTextColorPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
((OverflowLabel)bindable).SetTextColor((Color)newValue);
}
static object FontSizeDefaultValueCreator(BindableObject bindable)
{
return ((IFontElement)bindable).FontSizeDefaultValueCreator();
}
Label label;
public TextAlignment HorizontalTextAlignment
{
get => (TextAlignment)GetValue(HorizontalTextAlignmentProperty);
set
{
SetValue(HorizontalTextAlignmentProperty, value);
SetHorizontalTextAlignment(value);
}
}
public string Text
{
get => (string)GetValue(TextProperty);
set
{
SetValue(TextProperty, value);
SetText(value);
}
}
public Font Font
{
get => (Font)GetValue(FontProperty);
set
{
SetValue(FontProperty, value);
SetFont(value);
}
}
public Color TextColor
{
get { return (Color)GetValue(TextColorProperty); }
set { SetValue(TextColorProperty, value); }
}
public FontAttributes FontAttributes
{
get => (FontAttributes)GetValue(FontAttributesProperty);
set
{
SetValue(FontAttributesProperty, value);
SetFontAttributes(value);
}
}
public string FontFamily
{
get => (string)GetValue(FontFamilyProperty);
set
{
SetValue(FontFamilyProperty, value);
SetFontFamily(value);
}
}
[TypeConverter(typeof(FontSizeConverter))]
public double FontSize
{
get => (double)GetValue(FontSizeProperty);
set
{
SetValue(FontSizeProperty, value);
SetFontSize(value);
}
}
void SetText(string text) => label.Text = text;
void SetHorizontalTextAlignment(TextAlignment textAlignment) => label.HorizontalTextAlignment = textAlignment;
void SetFont(Font font) => label.Font = font;
void SetFontAttributes(FontAttributes fontAttributes) => label.FontAttributes = fontAttributes;
void SetFontFamily(string fontFamily) => label.FontFamily = fontFamily;
void SetFontSize(double fontSize) => label.FontSize = fontSize;
void SetTextColor(Color color) => label.TextColor = color;
public OverflowLabel()
{
this.HorizontalOptions = LayoutOptions.FillAndExpand;
label = new Label
{
HorizontalTextAlignment = HorizontalTextAlignment,
Text = Text,
HorizontalOptions = HorizontalOptions,
};
Content = label;
}
protected override SizeRequest OnMeasure(double widthConstraint, double heightConstraint)
{
if (label != null)
return Device.PlatformServices.GetNativeSize(label, widthConstraint, heightConstraint);
return base.OnMeasure(widthConstraint, heightConstraint);
}
public void OnFontFamilyChanged(string oldValue, string newValue) => SetFontFamily(newValue);
public void OnFontSizeChanged(double oldValue, double newValue) => SetFontSize(newValue);
double IFontElement.FontSizeDefaultValueCreator() =>
Device.GetNamedSize(NamedSize.Default, label);
public void OnFontAttributesChanged(FontAttributes oldValue, FontAttributes newValue) => SetFontAttributes(newValue);
public void OnFontChanged(Font oldValue, Font newValue) => SetFont(newValue);
//public static implicit operator Label (OverflowLabel overflow) => overflow.label;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment