diff --git a/AiForms.Effects.Droid/AddTextPlatformEffect.cs b/AiForms.Effects.Droid/AddTextPlatformEffect.cs index 8d0e1b2..af70bba 100644 --- a/AiForms.Effects.Droid/AddTextPlatformEffect.cs +++ b/AiForms.Effects.Droid/AddTextPlatformEffect.cs @@ -29,9 +29,9 @@ protected override void OnAttached() UpdateText(); UpdateFontSize(); UpdateTextColor(); - UpdateMargin(); - UpdateHorizontalAlign(); - UpdateVerticalAlign(); + UpdateBackgroundColor(); + UpdatePadding(); + Container.RequestLayout(); } protected override void OnDetached() @@ -46,7 +46,6 @@ protected override void OnDetached() _textView.Dispose(); _textView = null; - } protected override void OnElementPropertyChanged(System.ComponentModel.PropertyChangedEventArgs args) @@ -54,23 +53,30 @@ protected override void OnElementPropertyChanged(System.ComponentModel.PropertyC base.OnElementPropertyChanged(args); if (args.PropertyName == AddText.TextProperty.PropertyName) { UpdateText(); + Container.RequestLayout(); } 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(); } } @@ -92,26 +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() { - _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 { @@ -128,11 +130,42 @@ 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) { - _textview.Right = v.Width; + if (string.IsNullOrEmpty(_textview.Text)) { + return; + } var margin = AddText.GetMargin(_element); - var height = (int)Forms.Context.ToPixels(margin.Top) + (int)Forms.Context.ToPixels(margin.Bottom) + _textview.LineHeight; - var yPos = AddText.GetVerticalAlign(_element) == Xamarin.Forms.TextAlignment.Start ? 0 : v.Height - height; + margin.Left = (int)Forms.Context.ToPixels(margin.Left); + margin.Top = (int)Forms.Context.ToPixels(margin.Top); + margin.Right = (int)Forms.Context.ToPixels(margin.Right); + margin.Bottom = (int)Forms.Context.ToPixels(margin.Bottom); + + var textpaint = _textview.Paint; + var rect = new Android.Graphics.Rect(); + 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 - (int)margin.Right - 4; + if (xPos < (int)margin.Left) { + xPos = (int)margin.Left; + } + _textview.Right = v.Width - (int)margin.Right; + } + else { + xPos = (int)margin.Left; + _textview.Right = (int)margin.Left + rect.Width() + _textview.PaddingLeft + _textview.PaddingRight + 4; + if (_textview.Right >= v.Width) { + _textview.Right = v.Width - (int)margin.Right; + } + } + + _textview.Left = xPos; + + + var fm = textpaint.GetFontMetrics(); + var height = (int)(Math.Abs(fm.Top) + fm.Bottom + _textview.PaddingTop + _textview.PaddingEnd); + var yPos = AddText.GetVerticalAlign(_element) == Xamarin.Forms.TextAlignment.Start ? 0 + (int)margin.Top : v.Height - height - (int)margin.Bottom; _textview.Top = yPos; _textview.Bottom = yPos + height; @@ -149,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 1896fde..0f96ea5 100644 --- a/AiForms.Effects.Droid/AiForms.Effects.Droid.csproj +++ b/AiForms.Effects.Droid/AiForms.Effects.Droid.csproj @@ -15,7 +15,6 @@ Assets true - 0.0.5-pre true @@ -68,19 +67,19 @@ ..\packages\Xamarin.Android.Support.v7.MediaRouter.23.3.0\lib\MonoAndroid403\Xamarin.Android.Support.v7.MediaRouter.dll - ..\packages\Xamarin.Forms.2.3.4.231\lib\MonoAndroid10\FormsViewGroup.dll + ..\..\Samples\ValidationSample\packages\Xamarin.Forms.2.3.4.247\lib\MonoAndroid10\FormsViewGroup.dll - ..\packages\Xamarin.Forms.2.3.4.231\lib\MonoAndroid10\Xamarin.Forms.Core.dll + ..\..\Samples\ValidationSample\packages\Xamarin.Forms.2.3.4.247\lib\MonoAndroid10\Xamarin.Forms.Core.dll - ..\packages\Xamarin.Forms.2.3.4.231\lib\MonoAndroid10\Xamarin.Forms.Platform.Android.dll + ..\..\Samples\ValidationSample\packages\Xamarin.Forms.2.3.4.247\lib\MonoAndroid10\Xamarin.Forms.Platform.Android.dll - ..\packages\Xamarin.Forms.2.3.4.231\lib\MonoAndroid10\Xamarin.Forms.Platform.dll + ..\..\Samples\ValidationSample\packages\Xamarin.Forms.2.3.4.247\lib\MonoAndroid10\Xamarin.Forms.Platform.dll - ..\packages\Xamarin.Forms.2.3.4.231\lib\MonoAndroid10\Xamarin.Forms.Xaml.dll + ..\..\Samples\ValidationSample\packages\Xamarin.Forms.2.3.4.247\lib\MonoAndroid10\Xamarin.Forms.Xaml.dll @@ -101,13 +100,8 @@ - - - {14FCC004-FC52-4028-A5AE-228AFD6F6222} - AiForms.Effects - - + \ No newline at end of file diff --git a/AiForms.Effects.Droid/packages.config b/AiForms.Effects.Droid/packages.config index e9d994e..f7c2ff2 100644 --- a/AiForms.Effects.Droid/packages.config +++ b/AiForms.Effects.Droid/packages.config @@ -8,5 +8,5 @@ - + \ No newline at end of file diff --git a/AiForms.Effects.iOS/AddTextPlatformEffect.cs b/AiForms.Effects.iOS/AddTextPlatformEffect.cs index 3f9b971..2dd8be4 100644 --- a/AiForms.Effects.iOS/AddTextPlatformEffect.cs +++ b/AiForms.Effects.iOS/AddTextPlatformEffect.cs @@ -5,19 +5,19 @@ using Xamarin.Forms; using UIKit; using System.Linq; +using CoreGraphics; [assembly: ExportEffect(typeof(AddTextPlatformEffect), nameof(AddText))] namespace AiForms.Effects.iOS { public class AddTextPlatformEffect : PlatformEffect { - private UILabel _textLabel; + private PaddingLabel _textLabel; private NSLayoutConstraint[] _constraint; - private Thickness _margin = 0; protected override void OnAttached() { - _textLabel = new UILabel(); + _textLabel = new PaddingLabel(); _textLabel.LineBreakMode = UILineBreakMode.Clip; _textLabel.Lines = 1; _textLabel.TintAdjustmentMode = UIViewTintAdjustmentMode.Automatic; @@ -32,9 +32,9 @@ protected override void OnAttached() UpdateText(); UpdateFontSize(); UpdateTextColor(); - UpdateMargin(); - UpdateHorizontalAlign(); - UpdateVerticalAlign(); + UpdateBackgroundColor(); + UpdatePadding(); + UpdateConstraint(); } protected override void OnDetached() @@ -43,6 +43,7 @@ protected override void OnDetached() _textLabel.RemoveFromSuperview(); _textLabel.Dispose(); _constraint = null; + _textLabel = null; } protected override void OnElementPropertyChanged(System.ComponentModel.PropertyChangedEventArgs args) @@ -57,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(); } } @@ -73,7 +80,6 @@ void UpdateText() var text = AddText.GetText(Element); _textLabel.Text = text; _textLabel.Hidden = string.IsNullOrEmpty(text); - } void UpdateFontSize() @@ -86,55 +92,53 @@ 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 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, + isLeft ? NSLayoutAttribute.Left : NSLayoutAttribute.Right, NSLayoutRelation.Equal, Container, - NSLayoutAttribute.Left, + isLeft ? NSLayoutAttribute.Left : NSLayoutAttribute.Right, 1, - (nfloat)margin.Left + isLeft ? (nfloat)margin.Left : -(nfloat)margin.Right ), NSLayoutConstraint.Create( _textLabel, - NSLayoutAttribute.Right, - NSLayoutRelation.Equal, + NSLayoutAttribute.Width, + NSLayoutRelation.LessThanOrEqual, Container, - NSLayoutAttribute.Right, - 1, - -(nfloat)margin.Right - ), - NSLayoutConstraint.Create( - _textLabel, - NSLayoutAttribute.Height, - NSLayoutRelation.Equal, - null, - NSLayoutAttribute.Height, + NSLayoutAttribute.Width, 1, - (float)AddText.GetFontSize(Element) + -(nfloat)(margin.Left + margin.Right) ), NSLayoutConstraint.Create( _textLabel, @@ -151,6 +155,25 @@ NSLayoutConstraint[] CreateConstraint(Thickness margin, bool isTop = true) } } + internal class PaddingLabel : UILabel + { + public UIEdgeInsets Padding { get; set; } + + public override void DrawText(CoreGraphics.CGRect rect) + { + base.DrawText(Padding.InsetRect(rect)); + } + + public override CGSize IntrinsicContentSize { + get { + return new CGSize( + base.IntrinsicContentSize.Width + Padding.Left + Padding.Right, + base.IntrinsicContentSize.Height + Padding.Top + Padding.Bottom + ); + } + } + } + internal static class AlignmentExtensions { internal static UITextAlignment ToNativeTextAlignment(this TextAlignment alignment) diff --git a/AiForms.Effects.iOS/AiForms.Effects.iOS.csproj b/AiForms.Effects.iOS/AiForms.Effects.iOS.csproj index 800cf52..d832105 100644 --- a/AiForms.Effects.iOS/AiForms.Effects.iOS.csproj +++ b/AiForms.Effects.iOS/AiForms.Effects.iOS.csproj @@ -9,7 +9,6 @@ AiForms.Effects.iOS AiForms.Effects.iOS Resources - 0.0.5-pre true @@ -46,16 +45,16 @@ - ..\packages\Xamarin.Forms.2.3.4.231\lib\Xamarin.iOS10\Xamarin.Forms.Core.dll + ..\..\Samples\ValidationSample\packages\Xamarin.Forms.2.3.4.247\lib\Xamarin.iOS10\Xamarin.Forms.Core.dll - ..\packages\Xamarin.Forms.2.3.4.231\lib\Xamarin.iOS10\Xamarin.Forms.Platform.dll + ..\..\Samples\ValidationSample\packages\Xamarin.Forms.2.3.4.247\lib\Xamarin.iOS10\Xamarin.Forms.Platform.dll - ..\packages\Xamarin.Forms.2.3.4.231\lib\Xamarin.iOS10\Xamarin.Forms.Platform.iOS.dll + ..\..\Samples\ValidationSample\packages\Xamarin.Forms.2.3.4.247\lib\Xamarin.iOS10\Xamarin.Forms.Platform.iOS.dll - ..\packages\Xamarin.Forms.2.3.4.231\lib\Xamarin.iOS10\Xamarin.Forms.Xaml.dll + ..\..\Samples\ValidationSample\packages\Xamarin.Forms.2.3.4.247\lib\Xamarin.iOS10\Xamarin.Forms.Xaml.dll @@ -72,15 +71,10 @@ - - - {14FCC004-FC52-4028-A5AE-228AFD6F6222} - AiForms.Effects - - + \ No newline at end of file diff --git a/AiForms.Effects.iOS/packages.config b/AiForms.Effects.iOS/packages.config index dfb0a0c..5f8f73c 100644 --- a/AiForms.Effects.iOS/packages.config +++ b/AiForms.Effects.iOS/packages.config @@ -1,4 +1,4 @@  - + \ No newline at end of file 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/AiForms.Effects/AiForms.Effects.csproj b/AiForms.Effects/AiForms.Effects.csproj index c3fd8b5..b7fa47e 100644 --- a/AiForms.Effects/AiForms.Effects.csproj +++ b/AiForms.Effects/AiForms.Effects.csproj @@ -11,7 +11,6 @@ AiForms.Effects v4.5 Profile259 - 0.0.5-pre true @@ -39,13 +38,13 @@ - ..\packages\Xamarin.Forms.2.3.4.231\lib\portable-win+net45+wp80+win81+wpa81+MonoAndroid10+Xamarin.iOS10+xamarinmac20\Xamarin.Forms.Core.dll + ..\..\Samples\ValidationSample\packages\Xamarin.Forms.2.3.4.247\lib\portable-win+net45+wp80+win81+wpa81+MonoAndroid10+Xamarin.iOS10+xamarinmac20\Xamarin.Forms.Core.dll - ..\packages\Xamarin.Forms.2.3.4.231\lib\portable-win+net45+wp80+win81+wpa81+MonoAndroid10+Xamarin.iOS10+xamarinmac20\Xamarin.Forms.Platform.dll + ..\..\Samples\ValidationSample\packages\Xamarin.Forms.2.3.4.247\lib\portable-win+net45+wp80+win81+wpa81+MonoAndroid10+Xamarin.iOS10+xamarinmac20\Xamarin.Forms.Platform.dll - ..\packages\Xamarin.Forms.2.3.4.231\lib\portable-win+net45+wp80+win81+wpa81+MonoAndroid10+Xamarin.iOS10+xamarinmac20\Xamarin.Forms.Xaml.dll + ..\..\Samples\ValidationSample\packages\Xamarin.Forms.2.3.4.247\lib\portable-win+net45+wp80+win81+wpa81+MonoAndroid10+Xamarin.iOS10+xamarinmac20\Xamarin.Forms.Xaml.dll @@ -53,4 +52,5 @@ + \ No newline at end of file diff --git a/AiForms.Effects/packages.config b/AiForms.Effects/packages.config index 33c4845..e18960f 100644 --- a/AiForms.Effects/packages.config +++ b/AiForms.Effects/packages.config @@ -1,4 +1,4 @@  - + \ No newline at end of file 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"> - +