Created
July 28, 2015 02:02
-
-
Save programmation/4cf8310b999975652277 to your computer and use it in GitHub Desktop.
Xamarin Forms Label control with rounded rects. Invisible for configurable value.
This file contains hidden or 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
// RoundRectLabel.cs | |
using System; | |
using Xamarin.Forms; | |
namespace App | |
{ | |
public class RoundRectLabel : Label | |
{ | |
public static readonly BindableProperty BorderColorProperty = | |
BindableProperty.Create<RoundRectLabel, Color> (p => p.BorderColor, Color.Transparent); | |
public static readonly BindableProperty BorderWidthProperty = | |
BindableProperty.Create<RoundRectLabel, float> (p => p.BorderWidth, 0); | |
public static readonly BindableProperty CornerRadiusProperty = | |
BindableProperty.Create<RoundRectLabel, float> (p => p.CornerRadius, 0); | |
public static readonly BindableProperty HiddenValueProperty = | |
BindableProperty.Create<RoundRectLabel, string> (p => p.HiddenValue, null); | |
protected override void OnBindingContextChanged () | |
{ | |
base.OnBindingContextChanged (); | |
UpdateVisible (); | |
} | |
public Color BorderColor | |
{ | |
get | |
{ | |
return (Color)GetValue (BorderColorProperty); | |
} | |
set | |
{ | |
SetValue (BorderColorProperty, value); | |
} | |
} | |
public float BorderWidth | |
{ | |
get | |
{ | |
return (float)GetValue (BorderWidthProperty); | |
} | |
set | |
{ | |
SetValue (BorderWidthProperty, value); | |
} | |
} | |
public float CornerRadius | |
{ | |
get | |
{ | |
return (float)GetValue (CornerRadiusProperty); | |
} | |
set | |
{ | |
SetValue (CornerRadiusProperty, value); | |
} | |
} | |
public string HiddenValue | |
{ | |
get | |
{ | |
return (string)GetValue(HiddenValueProperty); | |
} | |
set | |
{ | |
SetValue (HiddenValueProperty, value); | |
UpdateVisible (); | |
} | |
} | |
private void UpdateVisible () | |
{ | |
if (String.IsNullOrEmpty (Text) || Text != HiddenValue) | |
{ | |
IsVisible = true; | |
return; | |
} | |
IsVisible = false; | |
} | |
public RoundRectLabel () | |
{ | |
} | |
} | |
} | |
// RoundRectLabelRenderer.cs | |
using System; | |
using Xamarin.Forms; | |
using App; | |
using App.iOS; | |
using Xamarin.Forms.Platform.iOS; | |
using UIKit; | |
[assembly: ExportRenderer(typeof(RoundRectLabel), typeof(RoundRectLabelRenderer))] | |
namespace App.iOS | |
{ | |
public class RoundRectLabelRenderer : LabelRenderer | |
{ | |
public RoundRectLabelRenderer () | |
{ | |
} | |
protected override void OnElementPropertyChanged (object sender, System.ComponentModel.PropertyChangedEventArgs e) | |
{ | |
base.OnElementPropertyChanged (sender, e); | |
// var label = Element as RoundRectLabel; | |
if (e.PropertyName == RoundRectLabel.BackgroundColorProperty.PropertyName) | |
{ | |
SetNeedsLayout (); | |
return; | |
} | |
if (e.PropertyName == RoundRectLabel.BorderColorProperty.PropertyName) | |
{ | |
SetNeedsLayout (); | |
return; | |
} | |
if (e.PropertyName == RoundRectLabel.BorderWidthProperty.PropertyName) | |
{ | |
SetNeedsLayout (); | |
return; | |
} | |
if (e.PropertyName == RoundRectLabel.CornerRadiusProperty.PropertyName) | |
{ | |
SetNeedsLayout (); | |
return; | |
} | |
if (e.PropertyName == RoundRectLabel.HiddenValueProperty.PropertyName) | |
{ | |
SetNeedsLayout (); | |
return; | |
} | |
} | |
public override void LayoutSubviews () | |
{ | |
base.LayoutSubviews (); | |
var label = Element as RoundRectLabel; | |
Layer.BorderWidth = label.BorderWidth; | |
Layer.CornerRadius = label.CornerRadius; | |
Layer.BackgroundColor = label.BackgroundColor.ToCGColor (); | |
Layer.BorderColor = label.BorderColor.ToCGColor (); | |
} | |
} | |
} | |
// RoundRectLabelRenderer.cs | |
using System; | |
using Xamarin.Forms.Platform.Android; | |
using Android.Graphics.Drawables; | |
using Android.Views; | |
using Xamarin.Forms; | |
using App; | |
using App.Droid; | |
[assembly: ExportRenderer(typeof(RoundRectLabel), typeof(RoundRectLabelRenderer))] | |
namespace App.Droid | |
{ | |
public class RoundRectLabelRenderer : LabelRenderer | |
{ | |
public RoundRectLabelRenderer () | |
{ | |
} | |
protected override void OnElementChanged (ElementChangedEventArgs<Label> e) | |
{ | |
base.OnElementChanged (e); | |
if (e.NewElement == null) | |
{ | |
return; | |
} | |
if (Control != null) | |
{ | |
Control.Invalidate (); | |
} | |
} | |
protected override void OnElementPropertyChanged (object sender, System.ComponentModel.PropertyChangedEventArgs e) | |
{ | |
base.OnElementPropertyChanged (sender, e); | |
// var label = Element as RoundRectLabel; | |
if (e.PropertyName == RoundRectLabel.TextProperty.PropertyName) | |
{ | |
Control.Invalidate (); | |
return; | |
} | |
if (e.PropertyName == RoundRectLabel.BackgroundColorProperty.PropertyName) | |
{ | |
Control.Invalidate (); | |
return; | |
} | |
if (e.PropertyName == RoundRectLabel.BorderColorProperty.PropertyName) | |
{ | |
Control.Invalidate (); | |
return; | |
} | |
if (e.PropertyName == RoundRectLabel.BorderWidthProperty.PropertyName) | |
{ | |
Control.Invalidate (); | |
return; | |
} | |
if (e.PropertyName == RoundRectLabel.CornerRadiusProperty.PropertyName) | |
{ | |
Control.Invalidate (); | |
return; | |
} | |
if (e.PropertyName == RoundRectLabel.HiddenValueProperty.PropertyName) | |
{ | |
Control.Invalidate (); | |
return; | |
} | |
} | |
protected override void OnLayout (bool changed, int l, int t, int r, int b) | |
{ | |
base.OnLayout (changed, l, t, r, b); | |
var label = Element as RoundRectLabel; | |
var background = new Android.Graphics.Drawables.GradientDrawable (); | |
background.SetCornerRadius ((int)label.CornerRadius); | |
background.SetStroke ((int)label.BorderWidth, label.BorderColor.ToAndroid ()); | |
background.SetColor (label.BackgroundColor.ToAndroid ()); | |
SetBackgroundColor (Android.Graphics.Color.Transparent); | |
SetBackgroundDrawable (background); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
great! How about UWP!