Skip to content

Instantly share code, notes, and snippets.

@kvandake
Created May 3, 2017 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kvandake/44ac12a8afdbca670ffedae95997be14 to your computer and use it in GitHub Desktop.
Save kvandake/44ac12a8afdbca670ffedae95997be14 to your computer and use it in GitHub Desktop.
Xamarin: TextView with Placeholder
namespace Kvandake
{
using System;
using CoreGraphics;
using Foundation;
using UIKit;
[Register("PlaceholderTextView")]
public class PlaceholderTextView : UITextView
{
private bool isInitialized;
private UIColor placeholderColor;
private string placeholder;
UIEdgeInsets padding;
NSLayoutConstraint LeftConstraint;
NSLayoutConstraint RightConstraint;
NSLayoutConstraint TopConstraint;
NSLayoutConstraint BottomConstraint;
public bool AllowWhiteSpace { get; set; } = false;
public UILabel PlaceholderLabel { get; set; } = new UILabel { TranslatesAutoresizingMaskIntoConstraints = false };
public PlaceholderTextView(CGRect frame) : base(frame)
{
this.Initialize();
}
public PlaceholderTextView(IntPtr ptr) : base(ptr)
{
this.Initialize();
}
public UIColor PlaceholderColor
{
get { return this.placeholderColor; }
set
{
this.placeholderColor = value;
this.PlaceholderLabel.TextColor = value;
}
}
public string Placeholder
{
get { return this.placeholder; }
set
{
this.placeholder = value;
this.PlaceholderLabel.Text = value;
}
}
private void Initialize()
{
this.PlaceholderColor = UIColor.LightGray;
this.AddSubview(this.PlaceholderLabel);
this.LeftConstraint = NSLayoutConstraint.Create(this.PlaceholderLabel, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this, NSLayoutAttribute.Left, 1, 0);
this.RightConstraint = NSLayoutConstraint.Create(this, NSLayoutAttribute.Trailing, NSLayoutRelation.Equal, this.PlaceholderLabel, NSLayoutAttribute.Trailing, 1, 0);
this.TopConstraint = NSLayoutConstraint.Create(this.PlaceholderLabel, NSLayoutAttribute.Top, NSLayoutRelation.Equal, this, NSLayoutAttribute.Top, 1, 0);
this.BottomConstraint = NSLayoutConstraint.Create(this, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, this.PlaceholderLabel, NSLayoutAttribute.Bottom, 1, 0);
this.AddConstraints(new[] { this.LeftConstraint, this.BottomConstraint, this.RightConstraint, this.TopConstraint });
this.Padding = new UIEdgeInsets(8, 4, 8, 4);
this.Started += this.OnStarted;
this.Ended += this.OnEnded;
}
public bool IsEmpty => string.IsNullOrEmpty(this.Text);
public UIEdgeInsets Padding
{
get
{
return this.padding;
}
set
{
this.padding = value;
if (this.LeftConstraint != null)
{
this.LeftConstraint.Constant = value.Left;
}
if (this.RightConstraint != null)
{
this.RightConstraint.Constant = value.Right;
}
if (this.TopConstraint != null)
{
this.TopConstraint.Constant = value.Top;
}
if (this.BottomConstraint != null)
{
this.BottomConstraint.Constant = value.Bottom;
}
}
}
public override void LayoutSubviews()
{
base.LayoutSubviews();
if (this.PlaceholderLabel != null)
{
this.PlaceholderLabel.PreferredMaxLayoutWidth = this.TextContainer.Size.Width - this.TextContainer.LineFragmentPadding * 2.0f;
}
if (!this.isInitialized)
{
this.isInitialized = true;
this.PlaceholderLabel.Font = Font;
this.PlaceholderLabel.TextAlignment = this.TextAlignment;
this.PlaceholderLabel.Lines = 0;
this.PlaceholderLabel.BackgroundColor = UIColor.Clear;
}
}
private void OnStarted(object sender, EventArgs e)
{
this.PlaceholderLabel.Hidden = true;
}
private void OnEnded(object sender, EventArgs e)
{
if (this.AllowWhiteSpace)
{
this.PlaceholderLabel.Hidden = !string.IsNullOrEmpty(this.Text);
}
else
{
this.PlaceholderLabel.Hidden = !string.IsNullOrWhiteSpace(this.Text);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment