Skip to content

Commit

Permalink
Merge pull request #6 from muak/fixAddText
Browse files Browse the repository at this point in the history
AddText New Property BackgroundColor and Padding
  • Loading branch information
muak authored May 30, 2017
2 parents 9f2eb9d + b186ea7 commit 0e4527d
Show file tree
Hide file tree
Showing 12 changed files with 187 additions and 96 deletions.
87 changes: 61 additions & 26 deletions AiForms.Effects.Droid/AddTextPlatformEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ protected override void OnAttached()
UpdateText();
UpdateFontSize();
UpdateTextColor();
UpdateMargin();
UpdateHorizontalAlign();
UpdateVerticalAlign();
UpdateBackgroundColor();
UpdatePadding();
Container.RequestLayout();
}

protected override void OnDetached()
Expand All @@ -46,31 +46,37 @@ protected override void OnDetached()

_textView.Dispose();
_textView = null;

}

protected override void OnElementPropertyChanged(System.ComponentModel.PropertyChangedEventArgs args)
{
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();
}
}

Expand All @@ -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
{
Expand All @@ -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;
Expand All @@ -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;
}
Expand Down
18 changes: 6 additions & 12 deletions AiForms.Effects.Droid/AiForms.Effects.Droid.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
<MonoAndroidAssetsPrefix>Assets</MonoAndroidAssetsPrefix>
<AndroidUseLatestPlatformSdk>true</AndroidUseLatestPlatformSdk>
<AndroidTlsProvider></AndroidTlsProvider>
<ReleaseVersion>0.0.5-pre</ReleaseVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down Expand Up @@ -68,19 +67,19 @@
<HintPath>..\packages\Xamarin.Android.Support.v7.MediaRouter.23.3.0\lib\MonoAndroid403\Xamarin.Android.Support.v7.MediaRouter.dll</HintPath>
</Reference>
<Reference Include="FormsViewGroup">
<HintPath>..\packages\Xamarin.Forms.2.3.4.231\lib\MonoAndroid10\FormsViewGroup.dll</HintPath>
<HintPath>..\..\Samples\ValidationSample\packages\Xamarin.Forms.2.3.4.247\lib\MonoAndroid10\FormsViewGroup.dll</HintPath>
</Reference>
<Reference Include="Xamarin.Forms.Core">
<HintPath>..\packages\Xamarin.Forms.2.3.4.231\lib\MonoAndroid10\Xamarin.Forms.Core.dll</HintPath>
<HintPath>..\..\Samples\ValidationSample\packages\Xamarin.Forms.2.3.4.247\lib\MonoAndroid10\Xamarin.Forms.Core.dll</HintPath>
</Reference>
<Reference Include="Xamarin.Forms.Platform.Android">
<HintPath>..\packages\Xamarin.Forms.2.3.4.231\lib\MonoAndroid10\Xamarin.Forms.Platform.Android.dll</HintPath>
<HintPath>..\..\Samples\ValidationSample\packages\Xamarin.Forms.2.3.4.247\lib\MonoAndroid10\Xamarin.Forms.Platform.Android.dll</HintPath>
</Reference>
<Reference Include="Xamarin.Forms.Platform">
<HintPath>..\packages\Xamarin.Forms.2.3.4.231\lib\MonoAndroid10\Xamarin.Forms.Platform.dll</HintPath>
<HintPath>..\..\Samples\ValidationSample\packages\Xamarin.Forms.2.3.4.247\lib\MonoAndroid10\Xamarin.Forms.Platform.dll</HintPath>
</Reference>
<Reference Include="Xamarin.Forms.Xaml">
<HintPath>..\packages\Xamarin.Forms.2.3.4.231\lib\MonoAndroid10\Xamarin.Forms.Xaml.dll</HintPath>
<HintPath>..\..\Samples\ValidationSample\packages\Xamarin.Forms.2.3.4.247\lib\MonoAndroid10\Xamarin.Forms.Xaml.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
Expand All @@ -101,13 +100,8 @@
<ItemGroup>
<AndroidResource Include="Resources\values\Strings.xml" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AiForms.Effects\AiForms.Effects.csproj">
<Project>{14FCC004-FC52-4028-A5AE-228AFD6F6222}</Project>
<Name>AiForms.Effects</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.CSharp.targets" />
<Import Project="..\packages\Xamarin.Android.Support.Vector.Drawable.23.3.0\build\Xamarin.Android.Support.Vector.Drawable.targets" Condition="Exists('..\packages\Xamarin.Android.Support.Vector.Drawable.23.3.0\build\Xamarin.Android.Support.Vector.Drawable.targets')" />
<Import Project="..\packages\Xamarin.Forms.2.3.4.231\build\portable-win+net45+wp80+win81+wpa81+MonoAndroid10+Xamarin.iOS10+xamarinmac20\Xamarin.Forms.targets" Condition="Exists('..\packages\Xamarin.Forms.2.3.4.231\build\portable-win+net45+wp80+win81+wpa81+MonoAndroid10+Xamarin.iOS10+xamarinmac20\Xamarin.Forms.targets')" />
<Import Project="..\..\Samples\ValidationSample\packages\Xamarin.Forms.2.3.4.247\build\portable-win+net45+wp80+win81+wpa81+MonoAndroid10+Xamarin.iOS10+xamarinmac20\Xamarin.Forms.targets" Condition="Exists('..\..\Samples\ValidationSample\packages\Xamarin.Forms.2.3.4.247\build\portable-win+net45+wp80+win81+wpa81+MonoAndroid10+Xamarin.iOS10+xamarinmac20\Xamarin.Forms.targets')" />
</Project>
2 changes: 1 addition & 1 deletion AiForms.Effects.Droid/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
<package id="Xamarin.Android.Support.v7.MediaRouter" version="23.3.0" targetFramework="monoandroid70" />
<package id="Xamarin.Android.Support.v7.RecyclerView" version="23.3.0" targetFramework="monoandroid70" />
<package id="Xamarin.Android.Support.Vector.Drawable" version="23.3.0" targetFramework="monoandroid70" />
<package id="Xamarin.Forms" version="2.3.4.231" targetFramework="monoandroid71" />
<package id="Xamarin.Forms" version="2.3.4.247" targetFramework="monoandroid71" />
</packages>
91 changes: 57 additions & 34 deletions AiForms.Effects.iOS/AddTextPlatformEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,9 +32,9 @@ protected override void OnAttached()
UpdateText();
UpdateFontSize();
UpdateTextColor();
UpdateMargin();
UpdateHorizontalAlign();
UpdateVerticalAlign();
UpdateBackgroundColor();
UpdatePadding();
UpdateConstraint();
}

protected override void OnDetached()
Expand All @@ -43,6 +43,7 @@ protected override void OnDetached()
_textLabel.RemoveFromSuperview();
_textLabel.Dispose();
_constraint = null;
_textLabel = null;
}

protected override void OnElementPropertyChanged(System.ComponentModel.PropertyChangedEventArgs args)
Expand All @@ -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();
}
}

Expand All @@ -73,7 +80,6 @@ void UpdateText()
var text = AddText.GetText(Element);
_textLabel.Text = text;
_textLabel.Hidden = string.IsNullOrEmpty(text);

}

void UpdateFontSize()
Expand All @@ -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,
Expand All @@ -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)
Expand Down
Loading

0 comments on commit 0e4527d

Please sign in to comment.