diff options
author | Samantha Houts <samantha@teamredwall.com> | 2016-03-31 10:43:59 -0700 |
---|---|---|
committer | Samantha Houts <samantha@teamredwall.com> | 2016-03-31 10:43:59 -0700 |
commit | d22a5c7c32701aa6b830b09e52d8e272a9d0a97f (patch) | |
tree | df0b2e44d1d71fbc32b55cd16ead8ccb69d36e30 /Xamarin.Forms.Core | |
parent | 850591679f51ed27cf00707296156ab4ed363873 (diff) | |
parent | 17e2a4b94d5105b53c5875d9416d043f14313305 (diff) | |
download | xamarin-forms-d22a5c7c32701aa6b830b09e52d8e272a9d0a97f.tar.gz xamarin-forms-d22a5c7c32701aa6b830b09e52d8e272a9d0a97f.tar.bz2 xamarin-forms-d22a5c7c32701aa6b830b09e52d8e272a9d0a97f.zip |
Merge pull request #11 from xamarin/fix-bugzilla27417
Add options for specifying layout of button text/image content
Diffstat (limited to 'Xamarin.Forms.Core')
-rw-r--r-- | Xamarin.Forms.Core/Button.cs | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/Xamarin.Forms.Core/Button.cs b/Xamarin.Forms.Core/Button.cs index 4662105f..9c746c9e 100644 --- a/Xamarin.Forms.Core/Button.cs +++ b/Xamarin.Forms.Core/Button.cs @@ -1,4 +1,6 @@ using System; +using System.Diagnostics; +using System.Globalization; using System.Windows.Input; using Xamarin.Forms.Platform; @@ -12,6 +14,9 @@ namespace Xamarin.Forms public static readonly BindableProperty CommandParameterProperty = BindableProperty.Create("CommandParameter", typeof(object), typeof(Button), null, propertyChanged: (bindable, oldvalue, newvalue) => ((Button)bindable).CommandCanExecuteChanged(bindable, EventArgs.Empty)); + public static readonly BindableProperty ContentLayoutProperty = + BindableProperty.Create("ContentLayout", typeof(ButtonContentLayout), typeof(Button), new ButtonContentLayout(ButtonContentLayout.ImagePosition.Left, DefaultSpacing)); + public static readonly BindableProperty TextProperty = BindableProperty.Create("Text", typeof(string), typeof(Button), null, propertyChanged: (bindable, oldVal, newVal) => ((Button)bindable).InvalidateMeasure(InvalidationTrigger.MeasureChanged)); @@ -39,6 +44,8 @@ namespace Xamarin.Forms bool _cancelEvents; + const double DefaultSpacing = 10; + public Color BorderColor { get { return (Color)GetValue(BorderColorProperty); } @@ -57,6 +64,12 @@ namespace Xamarin.Forms set { SetValue(BorderWidthProperty, value); } } + public ButtonContentLayout ContentLayout + { + get { return (ButtonContentLayout)GetValue(ContentLayoutProperty); } + set { SetValue(ContentLayoutProperty, value); } + } + public ICommand Command { get { return (ICommand)GetValue(CommandProperty); } @@ -247,5 +260,72 @@ namespace Xamarin.Forms button._cancelEvents = false; } + + [DebuggerDisplay("Image Position = {Position}, Spacing = {Spacing}")] + [TypeConverter(typeof(ButtonContentTypeConverter))] + public sealed class ButtonContentLayout + { + public enum ImagePosition + { + Left, + Top, + Right, + Bottom + } + + public ButtonContentLayout(ImagePosition position, double spacing) + { + Position = position; + Spacing = spacing; + } + + public ImagePosition Position { get; } + + public double Spacing { get; } + + public override string ToString() + { + return $"Image Position = {Position}, Spacing = {Spacing}"; + } + } + + public sealed class ButtonContentTypeConverter : TypeConverter + { + public override object ConvertFromInvariantString(string value) + { + if (value == null) + { + throw new InvalidOperationException($"Cannot convert null into {typeof(ButtonContentLayout)}"); + } + + string[] parts = value.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); + + if (parts.Length != 1 && parts.Length != 2) + { + throw new InvalidOperationException($"Cannot convert \"{value}\" into {typeof(ButtonContentLayout)}"); + } + + double spacing = DefaultSpacing; + var position = ButtonContentLayout.ImagePosition.Left; + + var spacingFirst = char.IsDigit(parts[0][0]); + + int positionIndex = spacingFirst ? (parts.Length == 2 ? 1 : -1) : 0; + int spacingIndex = spacingFirst ? 0 : (parts.Length == 2 ? 1 : -1); + + if (spacingIndex > -1) + { + spacing = double.Parse(parts[spacingIndex]); + } + + if (positionIndex > -1) + { + position = + (ButtonContentLayout.ImagePosition)Enum.Parse(typeof(ButtonContentLayout.ImagePosition), parts[positionIndex], true); + } + + return new ButtonContentLayout(position, spacing); + } + } } }
\ No newline at end of file |