Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Xamarin.Forms Entry just with bottom border. Added IsValid and ErrorColor Properties to better handle Validation Scenarios
using System;
using Xamarin.Forms;
namespace YOURNAMESPACE
{
public class LineEntry : Entry
{
public static BindableProperty BorderColorProperty =
BindableProperty.Create(
nameof(BorderColor),
typeof(Color),
typeof(LineEntry),
Color.Default,
BindingMode.TwoWay);
public Color BorderColor
{
get { return (Color)GetValue(BorderColorProperty); }
set { SetValue(BorderColorProperty, value); }
}
new public static readonly BindableProperty FontSizeProperty =
BindableProperty.Create(
nameof(FontSize),
typeof(double),
typeof(LineEntry),
Font.Default.FontSize,
BindingMode.TwoWay);
new public double FontSize
{
get { return (double)GetValue(FontSizeProperty); }
set { SetValue(FontSizeProperty, value); }
}
new public static readonly BindableProperty PlaceholderColorProperty =
BindableProperty.Create(
nameof(PlaceholderColor),
typeof(Color),
typeof(LineEntry),
Color.Default,
BindingMode.TwoWay);
new public Color PlaceholderColor
{
get { return (Color)GetValue(PlaceholderColorProperty); }
set { SetValue(PlaceholderColorProperty, value); }
}
public static BindableProperty ErrorColorProperty =
BindableProperty.Create(
nameof(ErrorColor),
typeof(Color),
typeof(LineEntry),
Color.Default,
BindingMode.TwoWay);
public Color ErrorColor
{
get { return (Color)GetValue(ErrorColorProperty); }
set { SetValue(ErrorColorProperty, value); }
}
public static BindableProperty IsValidProperty =
BindableProperty.Create(
nameof(IsValid),
typeof(bool),
typeof(LineEntry),
false,
BindingMode.TwoWay);
public bool IsValid
{
get { return (bool)GetValue(IsValidProperty); }
set { SetValue(IsValidProperty, value); }
}
}
}
using System.ComponentModel;
using Android.Graphics;
using YOURNAMESPACE.Android;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
[assembly: ExportRenderer(typeof(LineEntry), typeof(LineEntryRenderer))]
namespace YOURNAMESPACE.Android;
{
public class LineEntryRenderer : EntryRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (Control == null || Element == null || e.OldElement != null) return;
if(e.NewElement != null) {
var element = (LineEntry)Element;
DrawAll(element);
}
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
var view = (LineEntry)Element;
if (e.PropertyName.Equals("IsValid") || e.PropertyName.Equals("ErrorColor"))
DrawAll(view);
if (e.PropertyName.Equals("BorderColor"))
DrawBorder(view);
if (e.PropertyName.Equals("FontSize"))
SetFontSize(view);
if (e.PropertyName.Equals("PlaceholderColor"))
SetPlaceholderTextColor(view);
}
protected void DrawBorder(LineEntry element) {
Invalidate();
var color = element.BorderColor.ToAndroid();
// If the element is invalid, and the ErrorColor has been set, change the color
if (!element.IsValid && element.ErrorColor != Xamarin.Forms.Color.Default)
color = element.ErrorColor.ToAndroid();
Control.Background.SetColorFilter(color, PorterDuff.Mode.SrcAtop);
}
protected void SetPlaceholderTextColor(LineEntry element) {
Control.SetHintTextColor(element.PlaceholderColor.ToAndroid());
}
protected void SetFontSize(LineEntry element) {
Control.SetTextSize(Android.Util.ComplexUnitType.Sp, (float)element.FontSize);
}
protected void DrawAll(LineEntry element) {
DrawBorder(element);
SetPlaceholderTextColor(element);
SetFontSize(element);
}
}
}
using System.ComponentModel;
using CoreAnimation;
using CoreGraphics;
using Foundation;
using UIKit;
using YOURNAMESPACE.iOS;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer (typeof(YOUTNAMESPACE.LineEntry), typeof(LineEntryRenderer))]
namespace YOURNAMESPACE.iOS
{
public class LineEntryRenderer: EntryRenderer
{
protected override void OnElementChanged (ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged (e);
if (Control != null) {
Control.BorderStyle = UITextBorderStyle.None;
var view = (Element as LineEntry);
if (view != null) {
DrawAll (view);
}
}
}
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (Control != null)
{
Control.BorderStyle = UITextBorderStyle.None;
var view = (Element as LineEntry);
if (view != null)
{
DrawBorder(view);
SetFontSize(view);
SetPlaceholderTextColor(view);
}
}
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
var view = (LineEntry)Element;
if (e.PropertyName.Equals("IsValid") || e.PropertyName.Equals("ErrorColor"))
DrawAll(view);
if (e.PropertyName.Equals("BorderColor"))
DrawBorder(view);
if (e.PropertyName.Equals("FontSize"))
SetFontSize(view);
if (e.PropertyName.Equals("PlaceholderColor"))
SetPlaceholderTextColor(view);
}
void DrawBorder(LineEntry element)
{
SetNeedsDisplay();
var color = element.BorderColor.ToCGColor();
// If the element is invalid, and the ErrorColor has been set, change the color
if (!element.IsValid && element.ErrorColor != Color.Default)
color = element.ErrorColor.ToCGColor();
_borderLayer = new CALayer
{
MasksToBounds = true,
BorderColor = color,
BorderWidth = 1.0f
};
Control.Layer.AddSublayer(_borderLayer);
Control.BorderStyle = UITextBorderStyle.None;
}
void SetFontSize(LineEntry element)
{
if (element.FontSize != Font.Default.FontSize)
Control.Font = UIFont.SystemFontOfSize((System.nfloat)element.FontSize);
else if (element.FontSize == Font.Default.FontSize)
Control.Font = UIFont.SystemFontOfSize(17f);
}
void SetPlaceholderTextColor(LineEntry element)
{
if (string.IsNullOrEmpty(element.Placeholder) == false && element.PlaceholderColor != Color.Default)
{
var placeholderString =
new NSAttributedString(
element.Placeholder,
new UIStringAttributes {
ForegroundColor = element.PlaceholderColor.ToUIColor()
});
Control.AttributedPlaceholder = placeholderString;
}
}
protected void DrawAll(LineEntry element)
{
DrawBorder(element);
SetPlaceholderTextColor(element);
SetFontSize(element);
}
}
}
@technololy
Copy link

technololy commented Sep 17, 2018

screenshot_20180917-114853

Hello

The ios worked with some tweaks but the android did not, see attached

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment