Skip to content

Commit

Permalink
Release 0.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
muak committed May 30, 2017
1 parent 2cb7a12 commit b186ea7
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 107 deletions.
69 changes: 35 additions & 34 deletions AiForms.Effects.Droid/AddTextPlatformEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -30,9 +29,9 @@ protected override void OnAttached()
UpdateText();
UpdateFontSize();
UpdateTextColor();
UpdateMargin();
UpdateHorizontalAlign();
UpdateVerticalAlign();
UpdateBackgroundColor();
UpdatePadding();
Container.RequestLayout();
}

protected override void OnDetached()
Expand All @@ -47,7 +46,6 @@ protected override void OnDetached()

_textView.Dispose();
_textView = null;

}

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

Expand All @@ -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
{
Expand All @@ -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;
}

Expand All @@ -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;
}
}
Expand All @@ -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;
}
Expand Down
6 changes: 0 additions & 6 deletions AiForms.Effects.Droid/AiForms.Effects.Droid.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,6 @@
<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')" />
Expand Down
82 changes: 27 additions & 55 deletions AiForms.Effects.iOS/AddTextPlatformEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public class AddTextPlatformEffect : PlatformEffect
{
private PaddingLabel _textLabel;
private NSLayoutConstraint[] _constraint;
private Thickness _margin = 0;

protected override void OnAttached()
{
Expand All @@ -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);

Expand All @@ -36,9 +32,9 @@ protected override void OnAttached()
UpdateText();
UpdateFontSize();
UpdateTextColor();
UpdateMargin();
UpdateHorizontalAlign();
UpdateVerticalAlign();
UpdateBackgroundColor();
UpdatePadding();
UpdateConstraint();
}

protected override void OnDetached()
Expand All @@ -47,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 @@ -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();
}
}

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

}

void UpdateFontSize()
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -178,7 +151,6 @@ NSLayoutConstraint[] CreateConstraint(Thickness margin, bool isTop = true)
)
};


return constraint;
}
}
Expand Down
6 changes: 0 additions & 6 deletions AiForms.Effects.iOS/AiForms.Effects.iOS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,6 @@
<Compile Include="NumberPickerSource.cs" />
<Compile Include="AddTextPlatformEffect.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AiForms.Effects\AiForms.Effects.csproj">
<Project>{14FCC004-FC52-4028-A5AE-228AFD6F6222}</Project>
<Name>AiForms.Effects</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
Expand Down
36 changes: 36 additions & 0 deletions AiForms.Effects/AddText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading

0 comments on commit b186ea7

Please sign in to comment.