Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

IOS Custom Renderer Entry Right Margin Not Working

$
0
0

Hi, im currently trying to duplicate an Entry for IOS to be the same as the Android Entry Style. The problem im facing is that when setting the Margins for the custom entry on ios the right margin does not seem to take effect. Here are some examples

    <StackLayout>
        <local:MyIOSEntry Placeholder="First Name" Margin="45,0,45,0"></local:MyIOSEntry>
    </StackLayout>

    <StackLayout>
        <toolkit:EntryLine
        WidthRequest="50"
                   Text="{Binding Email}"
                   Keyboard="Email"
                   Margin="45,0,45,0"
                   Placeholder="email address"
                   x:Name="EntryEmail"
                   StyleId="EmailTextField"
                   IsEnabled="{Binding IsNotBusy}"
                   BorderColor="#ECECEC">
        </toolkit:EntryLine>
    </StackLayout>

    <StackLayout>
        <Entry Placeholder="First Name" Margin="45,0,45,0"></Entry>
    </StackLayout>

OUTPUT

So on the first entry the right Margin for my custom renderer the right margin it not in its correct spot

the second entry i put a Xamarin Form Toolkit Entry which completely spans off the screen even with a right margin set.

the third entry works fine but the entry is ugly and need to duplicate an android style entry field

here is my IOS entry custom renderer as to which i also found on the forums.

protected override void OnElementChanged(ElementChangedEventArgs e)
{
base.OnElementChanged(e);

        if (Control != null)
        {
            Control.BorderStyle = UITextBorderStyle.None;

            var view = (Element as MyIOSEntry);
            if (view != null)
            {
                DrawBorder(view);
                SetFontSize(view);
                SetPlaceholderTextColor(view);
            }
        }
    }



    protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        base.OnElementPropertyChanged(sender, e);

        base.OnElementPropertyChanged(sender, e);

        var view = (MyIOSEntry)Element;

        if (e.PropertyName.Equals(view.BorderColor))
            DrawBorder(view);
        if (e.PropertyName.Equals(view.FontSize))
            SetFontSize(view);
        if (e.PropertyName.Equals(view.PlaceholderColor))
            SetPlaceholderTextColor(view);
    }


    void DrawBorder(MyIOSEntry view)
    {
        var borderLayer = new CALayer();
        borderLayer.MasksToBounds = true;
        borderLayer.Frame = new CoreGraphics.CGRect(0f, Frame.Height / 2, Frame.Width, 1f);
        borderLayer.BorderColor = view.BorderColor.ToCGColor();
        borderLayer.BorderWidth = 1.0f;

        Control.Layer.AddSublayer(borderLayer);
        Control.BorderStyle = UITextBorderStyle.None;
    }

    void SetFontSize(MyIOSEntry view)
    {
        if (view.FontSize != Font.Default.FontSize)
            Control.Font = UIFont.SystemFontOfSize((System.nfloat)view.FontSize);
        else if (view.FontSize == Font.Default.FontSize)
            Control.Font = UIFont.SystemFontOfSize(17f);
    }

    void SetPlaceholderTextColor(MyIOSEntry view)
    {
        if (string.IsNullOrEmpty(view.Placeholder) == false && view.PlaceholderColor != Color.Default)
        {
            var placeholderString = new NSAttributedString(view.Placeholder,
                                        new UIStringAttributes { ForegroundColor = view.PlaceholderColor.ToUIColor() });
            Control.AttributedPlaceholder = placeholderString;
        }
    }

}

}

Can anyone see why the right margin is not taking effect. seems like such a sime problem. i've tryied moving all the entrys and putting them in 1 stacklayout with the margin set in the stacklayout but has no difference.

thanks

The first entry




Viewing all articles
Browse latest Browse all 91519

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>