diff --git a/AiForms.Effects.Droid/AddTextPlatformEffect.cs b/AiForms.Effects.Droid/AddTextPlatformEffect.cs index 414ff93..af70bba 100644 --- a/AiForms.Effects.Droid/AddTextPlatformEffect.cs +++ b/AiForms.Effects.Droid/AddTextPlatformEffect.cs @@ -20,7 +20,6 @@ protected override void OnAttached() _textView.SetMaxLines(1); _textView.SetMinLines(1); _textView.Ellipsize = Android.Text.TextUtils.TruncateAt.End; - _textView.SetBackgroundColor(Android.Graphics.Color.Argb(128, 0, 0, 0)); Container.AddView(_textView); @@ -30,9 +29,9 @@ protected override void OnAttached() UpdateText(); UpdateFontSize(); UpdateTextColor(); - UpdateMargin(); - UpdateHorizontalAlign(); - UpdateVerticalAlign(); + UpdateBackgroundColor(); + UpdatePadding(); + Container.RequestLayout(); } protected override void OnDetached() @@ -47,7 +46,6 @@ protected override void OnDetached() _textView.Dispose(); _textView = null; - } protected override void OnElementPropertyChanged(System.ComponentModel.PropertyChangedEventArgs args) @@ -59,20 +57,26 @@ protected override void OnElementPropertyChanged(System.ComponentModel.PropertyC } else if (args.PropertyName == AddText.FontSizeProperty.PropertyName) { UpdateFontSize(); - Container.RequestFocus(); + Container.RequestLayout(); } else if (args.PropertyName == AddText.TextColorProperty.PropertyName) { UpdateTextColor(); } + else if (args.PropertyName == AddText.BackgroundColorProperty.PropertyName) { + UpdateBackgroundColor(); + } + else if (args.PropertyName == AddText.PaddingProperty.PropertyName) { + UpdatePadding(); + Container.RequestLayout(); + } else if (args.PropertyName == AddText.MarginProperty.PropertyName) { - UpdateMargin(); - Container.RequestFocus(); + Container.RequestLayout(); } else if (args.PropertyName == AddText.HorizontalAlignProperty.PropertyName) { - UpdateHorizontalAlign(); + Container.RequestLayout(); } else if (args.PropertyName == AddText.VerticalAlignProperty.PropertyName) { - UpdateVerticalAlign(); + Container.RequestLayout(); } } @@ -94,27 +98,22 @@ void UpdateTextColor() _textView.SetTextColor(AddText.GetTextColor(Element).ToAndroid()); } - void UpdateMargin() + void UpdateBackgroundColor() { - var margin = AddText.GetMargin(Element); - _textView.SetPadding( - (int)Container.Context.ToPixels(margin.Left), - (int)Container.Context.ToPixels(margin.Top), - (int)Container.Context.ToPixels(margin.Right), - (int)Container.Context.ToPixels(margin.Bottom) - ); + _textView.SetBackgroundColor(AddText.GetBackgroundColor(Element).ToAndroid()); } - void UpdateHorizontalAlign() + void UpdatePadding() { - Container.RequestLayout(); - //_textView.Gravity = AddText.GetHorizontalAlign(Element).ToHorizontalGravityFlags(); + var padding = AddText.GetPadding(Element); + _textView.SetPadding( + (int)Container.Context.ToPixels(padding.Left), + (int)Container.Context.ToPixels(padding.Top), + (int)Container.Context.ToPixels(padding.Right), + (int)Container.Context.ToPixels(padding.Bottom) + ); } - void UpdateVerticalAlign() - { - Container.RequestLayout(); - } internal class ContainerOnLayoutChangeListener : Java.Lang.Object, Android.Views.View.IOnLayoutChangeListener { @@ -131,7 +130,7 @@ public ContainerOnLayoutChangeListener(TextView textview, Element element) // For some reason, in layout that was added to container, it does not work all gravity options and all layout options. public void OnLayoutChange(Android.Views.View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { - if(string.IsNullOrEmpty(_textview.Text)){ + if (string.IsNullOrEmpty(_textview.Text)) { return; } @@ -146,17 +145,17 @@ public void OnLayoutChange(Android.Views.View v, int left, int top, int right, i textpaint.GetTextBounds(_textview.Text, 0, _textview.Text.Length, rect); var xPos = 0; - if(AddText.GetHorizontalAlign(_element) == Xamarin.Forms.TextAlignment.End){ - xPos = v.Width - rect.Width() - _textview.PaddingLeft - _textview.PaddingRight; - if (xPos < 0) { - xPos = 0; + if (AddText.GetHorizontalAlign(_element) == Xamarin.Forms.TextAlignment.End) { + xPos = v.Width - rect.Width() - _textview.PaddingLeft - _textview.PaddingRight - (int)margin.Right - 4; + if (xPos < (int)margin.Left) { + xPos = (int)margin.Left; } - _textview.Right = v.Right - (int)margin.Right; + _textview.Right = v.Width - (int)margin.Right; } - else{ + else { xPos = (int)margin.Left; - _textview.Right = (int)margin.Left + rect.Width() + _textview.PaddingLeft + _textview.PaddingRight + 2; - if(_textview.Right >= v.Width){ + _textview.Right = (int)margin.Left + rect.Width() + _textview.PaddingLeft + _textview.PaddingRight + 4; + if (_textview.Right >= v.Width) { _textview.Right = v.Width - (int)margin.Right; } } @@ -183,6 +182,8 @@ internal static GravityFlags ToHorizontalGravityFlags(this Xamarin.Forms.TextAli switch (alignment) { case Xamarin.Forms.TextAlignment.End: return GravityFlags.Right; + case Xamarin.Forms.TextAlignment.Center: + return GravityFlags.Center; default: return GravityFlags.Left; } diff --git a/AiForms.Effects.Droid/AiForms.Effects.Droid.csproj b/AiForms.Effects.Droid/AiForms.Effects.Droid.csproj index feaafc0..0f96ea5 100644 --- a/AiForms.Effects.Droid/AiForms.Effects.Droid.csproj +++ b/AiForms.Effects.Droid/AiForms.Effects.Droid.csproj @@ -100,12 +100,6 @@ - - - {14FCC004-FC52-4028-A5AE-228AFD6F6222} - AiForms.Effects - - diff --git a/AiForms.Effects.iOS/AddTextPlatformEffect.cs b/AiForms.Effects.iOS/AddTextPlatformEffect.cs index 32d78fd..2dd8be4 100644 --- a/AiForms.Effects.iOS/AddTextPlatformEffect.cs +++ b/AiForms.Effects.iOS/AddTextPlatformEffect.cs @@ -14,7 +14,6 @@ public class AddTextPlatformEffect : PlatformEffect { private PaddingLabel _textLabel; private NSLayoutConstraint[] _constraint; - private Thickness _margin = 0; protected override void OnAttached() { @@ -25,9 +24,6 @@ protected override void OnAttached() _textLabel.AdjustsFontSizeToFitWidth = true; _textLabel.BaselineAdjustment = UIBaselineAdjustment.AlignCenters; _textLabel.AdjustsLetterSpacingToFitWidth = true; - _textLabel.BackgroundColor = UIColor.FromRGBA(0, 0, 0, 0.5f); - _textLabel.Padding = new UIEdgeInsets(4,4,4,4); - Container.AddSubview(_textLabel); @@ -36,9 +32,9 @@ protected override void OnAttached() UpdateText(); UpdateFontSize(); UpdateTextColor(); - UpdateMargin(); - UpdateHorizontalAlign(); - UpdateVerticalAlign(); + UpdateBackgroundColor(); + UpdatePadding(); + UpdateConstraint(); } protected override void OnDetached() @@ -47,6 +43,7 @@ protected override void OnDetached() _textLabel.RemoveFromSuperview(); _textLabel.Dispose(); _constraint = null; + _textLabel = null; } protected override void OnElementPropertyChanged(System.ComponentModel.PropertyChangedEventArgs args) @@ -61,14 +58,20 @@ protected override void OnElementPropertyChanged(System.ComponentModel.PropertyC else if (args.PropertyName == AddText.TextColorProperty.PropertyName) { UpdateTextColor(); } + else if (args.PropertyName == AddText.BackgroundColorProperty.PropertyName) { + UpdateBackgroundColor(); + } + else if (args.PropertyName == AddText.PaddingProperty.PropertyName) { + UpdatePadding(); + } else if (args.PropertyName == AddText.MarginProperty.PropertyName) { - UpdateMargin(); + UpdateConstraint(); } else if (args.PropertyName == AddText.HorizontalAlignProperty.PropertyName) { - UpdateHorizontalAlign(); + UpdateConstraint(); } else if (args.PropertyName == AddText.VerticalAlignProperty.PropertyName) { - UpdateVerticalAlign(); + UpdateConstraint(); } } @@ -77,7 +80,6 @@ void UpdateText() var text = AddText.GetText(Element); _textLabel.Text = text; _textLabel.Hidden = string.IsNullOrEmpty(text); - } void UpdateFontSize() @@ -90,55 +92,36 @@ void UpdateTextColor() _textLabel.TextColor = AddText.GetTextColor(Element).ToUIColor(); } - void UpdateMargin() + void UpdateBackgroundColor() { - _margin = AddText.GetMargin(Element); + _textLabel.BackgroundColor = AddText.GetBackgroundColor(Element).ToUIColor(); } - void UpdateHorizontalAlign() + void UpdatePadding() { - //_textLabel.TextAlignment = AddText.GetHorizontalAlign(Element).ToNativeTextAlignment(); - //var align = AddText.GetHorizontalAlign(Element); - //if (_constraint != null) { - // Container.RemoveConstraints(_constraint); - //} - //_constraint = CreateConstraint(_margin, align == TextAlignment.Start,); - //Container.AddConstraints(_constraint); - UpdateVerticalAlign(); + var padding = AddText.GetPadding(Element); + _textLabel.Padding = new UIEdgeInsets((float)padding.Top, (float)padding.Left, (float)padding.Bottom, (float)padding.Right); } - void UpdateVerticalAlign() + void UpdateConstraint() { - var align = AddText.GetVerticalAlign(Element); + if (_constraint != null) { Container.RemoveConstraints(_constraint); } - _constraint = CreateConstraint(_margin, align == TextAlignment.Start); + _constraint = CreateConstraint(); Container.AddConstraints(_constraint); } - NSLayoutConstraint[] CreateConstraint(Thickness margin, bool isTop = true) + NSLayoutConstraint[] CreateConstraint() { var isLeft = AddText.GetHorizontalAlign(Element) == Xamarin.Forms.TextAlignment.Start; + var isTop = AddText.GetVerticalAlign(Element) == Xamarin.Forms.TextAlignment.Start; + var margin = AddText.GetMargin(Element); + + _textLabel.TextAlignment = isLeft ? UITextAlignment.Left : UITextAlignment.Right; + var constraint = new NSLayoutConstraint[]{ - //NSLayoutConstraint.Create( - // _textLabel, - // NSLayoutAttribute.Left, - // NSLayoutRelation.Equal, - // Container, - // NSLayoutAttribute.Left, - // 1, - // (nfloat)margin.Left - //), - //NSLayoutConstraint.Create( - // _textLabel, - // NSLayoutAttribute.Right, - // NSLayoutRelation.Equal, - // Container, - // NSLayoutAttribute.Right, - // 1, - // -(nfloat)margin.Right - //), NSLayoutConstraint.Create( _textLabel, isLeft ? NSLayoutAttribute.Left : NSLayoutAttribute.Right, @@ -157,16 +140,6 @@ NSLayoutConstraint[] CreateConstraint(Thickness margin, bool isTop = true) 1, -(nfloat)(margin.Left + margin.Right) ), - //NSLayoutConstraint.Create( - // _textLabel, - // NSLayoutAttribute.Height, - // NSLayoutRelation.Equal, - // null, - // NSLayoutAttribute.Height, - // 1, - // (float)AddText.GetFontSize(Element) + (float)margin.Top + (float)margin.Bottom - //), - NSLayoutConstraint.Create( _textLabel, isTop ? NSLayoutAttribute.Top : NSLayoutAttribute.Bottom, @@ -178,7 +151,6 @@ NSLayoutConstraint[] CreateConstraint(Thickness margin, bool isTop = true) ) }; - return constraint; } } diff --git a/AiForms.Effects.iOS/AiForms.Effects.iOS.csproj b/AiForms.Effects.iOS/AiForms.Effects.iOS.csproj index cfac484..d832105 100644 --- a/AiForms.Effects.iOS/AiForms.Effects.iOS.csproj +++ b/AiForms.Effects.iOS/AiForms.Effects.iOS.csproj @@ -71,12 +71,6 @@ - - - {14FCC004-FC52-4028-A5AE-228AFD6F6222} - AiForms.Effects - - diff --git a/AiForms.Effects/AddText.cs b/AiForms.Effects/AddText.cs index f7433df..e98de9c 100644 --- a/AiForms.Effects/AddText.cs +++ b/AiForms.Effects/AddText.cs @@ -96,6 +96,42 @@ public static Color GetTextColor(BindableObject view) return (Color)view.GetValue(TextColorProperty); } + public static readonly BindableProperty BackgroundColorProperty = + BindableProperty.CreateAttached( + "BackgroundColor", + typeof(Color), + typeof(AddText), + Color.Transparent + ); + + public static void SetBackgroundColor(BindableObject view, Color value) + { + view.SetValue(BackgroundColorProperty, value); + } + + public static Color GetBackgroundColor(BindableObject view) + { + return (Color)view.GetValue(BackgroundColorProperty); + } + + public static readonly BindableProperty PaddingProperty = + BindableProperty.CreateAttached( + "Padding", + typeof(Thickness), + typeof(AddText), + default(Thickness) + ); + + public static void SetPadding(BindableObject view, Thickness value) + { + view.SetValue(PaddingProperty, value); + } + + public static Thickness GetPadding(BindableObject view) + { + return (Thickness)view.GetValue(PaddingProperty); + } + public static readonly BindableProperty MarginProperty = BindableProperty.CreateAttached( "Margin", diff --git a/README.md b/README.md index a4f23f9..44be6b1 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Xamarin.Forms Effects for Android / iOS only. ## Features -* [AddText](#addtext)(from 0.1.1-pre) +* [AddText](#addtext) * add one line Text to a view. * [ToFlatButton](#toflatbutton) * alter Button to flat (for Android) @@ -70,9 +70,16 @@ and more. * added text * TextColor * Default Red +* BackgroundColor + * BackgroundColor of inner text view. + * Default Transparent * FontSize * Default 8 * Margin + * Distance from a side of target view to inner text view. + * Default 0,0,0,0 +* Padding + * Padding of inner text view. * Default 0,0,0,0 * HorizontalAlign * horizontal text position(Start or End). Default End. @@ -91,8 +98,9 @@ and more. diff --git a/Tests/AiEffects.TestApp/AiEffects.TestApp/Views/AddTextPage.xaml b/Tests/AiEffects.TestApp/AiEffects.TestApp/Views/AddTextPage.xaml index cbcc0b7..74a77a5 100644 --- a/Tests/AiEffects.TestApp/AiEffects.TestApp/Views/AddTextPage.xaml +++ b/Tests/AiEffects.TestApp/AiEffects.TestApp/Views/AddTextPage.xaml @@ -6,7 +6,7 @@ BackgroundColor="#EEEEEE" x:Class="AiEffects.TestApp.Views.AddTextPage" Title="AddText"> - +