Skip to content

Instantly share code, notes, and snippets.

@martinbowling
Created March 26, 2010 18:50
Show Gist options
  • Save martinbowling/345233 to your computer and use it in GitHub Desktop.
Save martinbowling/345233 to your computer and use it in GitHub Desktop.
public class EntryElement : Element {
/// <summary>
/// The value of the EntryElement
/// </summary>
public string Value;
/// <summary>
/// The type of keyboard used for input, you can change
/// this to use this for numeric input, email addressed,
/// urls, phones.
/// </summary>
public UIKeyboardType KeyboardType = UIKeyboardType.Default;
static NSString ekey = new NSString ("EntryElement");
bool isPassword;
UITextField entry;
string placeholder;
static UIFont font = UIFont.BoldSystemFontOfSize (17);
public Func<bool,string> Validator;
/// <summary>
/// Constructs an EntryElement with the given caption, placeholder and initial value.
/// </summary>
/// <param name="caption">
/// The caption to use
/// </param>
/// <param name="placeholder">
/// Placeholder to display.
/// </param>
/// <param name="value">
/// Initial value.
/// </param>
public EntryElement (string caption, string placeholder, string value) : base (caption)
{
Value = value;
this.placeholder = placeholder;
}
/// <summary>
/// Constructs an EntryElement for password entry with the given caption, placeholder and initial value.
/// </summary>
/// <param name="caption">
/// The caption to use
/// </param>
/// <param name="placeholder">
/// Placeholder to display.
/// </param>
/// <param name="value">
/// Initial value.
/// </param>
/// <param name="isPassword">
/// True if this should be used to enter a password.
/// </param>
public EntryElement (string caption, string placeholder, string value, bool isPassword) : base (caption)
{
Value = value;
this.isPassword = isPassword;
this.placeholder = placeholder;
}
public override string Summary ()
{
return Value;
}
//
// Computes the X position for the entry by aligning all the entries in the Section
//
SizeF ComputeEntryPosition (UITableView tv, UITableViewCell cell)
{
Section s = Parent as Section;
if (s.EntryAlignment.Width != 0)
return s.EntryAlignment;
SizeF max = new SizeF (-1, -1);
foreach (var e in s.Elements){
var ee = e as EntryElement;
if (ee == null)
continue;
var size = tv.StringSize (ee.Caption, font);
if (size.Width > max.Width)
max = size;
}
s.EntryAlignment = new SizeF (25 + Math.Min (max.Width, 160), max.Height);
return s.EntryAlignment;
}
public override UITableViewCell GetCell (UITableView tv)
{
var cell = tv.DequeueReusableCell (ekey);
if (cell == null){
cell = new UITableViewCell (UITableViewCellStyle.Default, ekey);
cell.SelectionStyle = UITableViewCellSelectionStyle.None;
} else
RemoveTag (cell, 1);
if (entry == null){
SizeF size = ComputeEntryPosition (tv, cell);
entry = new UITextField (new RectangleF (size.Width, (cell.ContentView.Bounds.Height-size.Height)/2-1, 320-size.Width, size.Height)){
Tag = 1,
Placeholder = placeholder ?? "",
SecureTextEntry = isPassword
};
entry.Text = Value ?? "";
entry.AutoresizingMask = UIViewAutoresizing.FlexibleWidth |
UIViewAutoresizing.FlexibleLeftMargin;
entry.ValueChanged += delegate {
Value = entry.Text;
};
entry.Ended += delegate {
Value = entry.Text;
};
entry.ShouldReturn += delegate {
EntryElement focus = null;
foreach (var e in (Parent as Section).Elements){
if (e == this)
focus = this;
else if (focus != null && e is EntryElement)
focus = e as EntryElement;
}
if (focus != this)
focus.entry.BecomeFirstResponder ();
else
focus.entry.ResignFirstResponder ();
return true;
};
entry.Started += delegate {
EntryElement self = null;
var returnType = UIReturnKeyType.Default;
foreach (var e in (Parent as Section).Elements){
if (e == this)
self = this;
else if (self != null && e is EntryElement)
returnType = UIReturnKeyType.Next;
}
entry.ReturnKeyType = returnType;
};
}
entry.KeyboardType = KeyboardType;
cell.TextLabel.Text = Caption;
cell.ContentView.AddSubview (entry);
return cell;
}
protected override void Dispose (bool disposing)
{
if (disposing){
entry.Dispose ();
entry = null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment