In my Xamarin forms application, I need to set a top padding for Entry control in iOS. I created renderers for Entry , but only I am able to set Left and Right padding. Please help me. Following is the my Entry Renderer
public class CustomRenderer: EntryRenderer
{
protected override void OnElementChanged (ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged (e);
if (Control != null) {
Control.LeftView = new UIView (new CGRect (0, 0, 15, 0));
Control.LeftViewMode = UITextFieldViewMode.Always;
}
}
}
Use this for setting padding to an entry cell:
Padding in IOS :
Control.LeftView = new UIView(new CGRect(0,15,0,0));
Control.LeftViewMode = UITextFieldViewMode.Always;
Control.RightView = new UIView(new CGRect(0,10, 0, 0));
Control.RightViewMode = UITextFieldViewMode.Always;
Padding in Android:
Control.SetPadding(0, 10, 0, 0);
Accordingly, you can set the value to make text start from specific position.
Entry
in Xamarin.FormsUITextField
In this example, I use a RelativeLayout
to set the height of the Padded Entry to 50, Constraint.Constant(50)
.
using System;
using Xamarin.Forms;
namespace CustomEntrySample
{
public class EntryWithTopPadding : Entry
{
}
public class App : Application
{
public App()
{
var normalEntry = new Entry
{
Text = "This Entry Has Normal Padding",
BackgroundColor = Color.Lime
};
var paddedEntry = new EntryWithTopPadding
{
Text = "This Entry Has Extra Top Padding",
BackgroundColor = Color.Aqua
};
var mainLayout = new RelativeLayout();
mainLayout.Children.Add(
normalEntry,
Constraint.Constant(0),
Constraint.RelativeToParent(parent => parent.Y + 10),
Constraint.RelativeToParent(parent => parent.Width)
);
mainLayout.Children.Add(
paddedEntry,
Constraint.Constant(0),
Constraint.RelativeToView(normalEntry, (parent, view) => view.Y + view.Height + 10),
Constraint.RelativeToParent(parent => parent.Width),
Constraint.Constant(50)
);
MainPage = new NavigationPage(
new ContentPage
{
Title = "Title",
Content = mainLayout,
Padding = new Thickness(10, 0, 10, 0)
}
);
}
}
}
Set the VerticalAlignment
property of the UITextField
to UIControlContentVerticalAlignment.Bottom
using UIKit;
using CustomEntrySample;
using CustomEntrySample.iOS;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer(typeof(EntryWithTopPadding), typeof(EntryWithTopPaddingCustomRenderer))]
namespace CustomEntrySample.iOS
{
public class EntryWithTopPaddingCustomRenderer : EntryRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if(Control == null)
return;
Control.VerticalAlignment = UIControlContentVerticalAlignment.Bottom;
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With