Skip to content

Instantly share code, notes, and snippets.

@RobertKozak
Created May 10, 2011 20:01
Show Gist options
  • Save RobertKozak/965257 to your computer and use it in GitHub Desktop.
Save RobertKozak/965257 to your computer and use it in GitHub Desktop.
DatePicker in InputView
//
// DateTimeElement.cs
//
// Author:
// Miguel de Icaza (miguel@gnome.org)
//
// Copyright 2010, Novell, Inc.
//
// Code licensed under the MIT X11 license
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
namespace MonoMobile.MVVM
{
using System;
using System.Drawing;
using MonoMobile.MVVM;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
public partial class DateTimeElement : Element, ISelectable, IFocusable
{
private UICustomTextField _Dummy { get; set; }
public UICustomTextField Entry { get; set; }
public DateTime Value { get; set; }
public UIDatePicker DatePicker;
protected NSDateFormatter fmt = new NSDateFormatter { DateStyle = NSDateFormatterStyle.Short };
public DateTimeElement(string caption) : base(caption)
{
}
public DateTimeElement(string caption, DateTime date) : base(caption)
{
Value = date;
}
public override UITableViewElementCell NewCell()
{
return new UITableViewElementCell(UITableViewCellStyle.Value1, Id, this);
}
public override void InitializeCell(UITableView tableView)
{
RemoveTag(2);
base.InitializeCell(tableView);
Cell.Accessory = UITableViewCellAccessory.None;
Cell.TextLabel.Text = Caption;
DetailTextLabel.Text = FormatDate(Value);
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposing)
{
fmt.Dispose();
fmt = null;
if (DatePicker != null)
{
DatePicker.Dispose();
DatePicker = null;
}
}
}
public virtual string FormatDate(DateTime dt)
{
return fmt.ToString(dt) + " " + dt.ToLocalTime().ToShortTimeString();
}
public override void InitializeContent()
{
_Dummy = new UICustomTextField(Bounds) { Tag = 1 };
_Dummy.ShouldBeginEditing = (tf) =>
{
Entry.BecomeFirstResponder();
return false;
};
Entry = new UICustomTextField(Bounds)
{
BackgroundColor = UIColor.Clear,
Tag = 2,
Hidden = true
};
DatePicker = CreatePicker();
var view = new UIView(DatePicker.Bounds);
view.AddSubview(DatePicker);
Entry.InputView = view;
Entry.InputAccessoryView = new UIKeyboardToolbar(this) { PreviousButtonVisible = false, NextButtonVisible = false };
Entry.Started += (s, e) =>
{
ValueProperty.ConvertBack<string>();
};
Entry.Ended += (s, e) =>
{
Value = DatePicker.Date;
OnValueChanged();
};
ContentView = _Dummy;
ContentView.AddSubview(Entry);
}
public void Selected(DialogViewController dvc, UITableView tableView, NSIndexPath path)
{
Entry.BecomeFirstResponder();
}
public virtual UIDatePicker CreatePicker()
{
var picker = new UIDatePicker(RectangleF.Empty) { AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight, Mode = UIDatePickerMode.Date, Date = (DateTime)Value };
return picker;
}
protected virtual void OnValueChanged()
{
if (DatePicker != null)
DatePicker.Date = Value;
if (DetailTextLabel != null)
DetailTextLabel.Text = FormatDate(Value);
}
public void MoveNext ()
{
throw new NotImplementedException();
}
public void MovePrev ()
{
throw new NotImplementedException();
}
}
}
//
// UIKeyboardToolbar.cs
//
// Author:
// Robert Kozak (rkozak@gmail.com / Twitter:@robertkozak)
//
// Copyright 2011, Nowcom Corporation.
//
// Code licensed under the MIT X11 license
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
namespace MonoMobile.MVVM
{
using System;
using System.Drawing;
using System.Linq;
using MonoTouch.Foundation;
using System.Collections.Generic;
using MonoTouch.UIKit;
public class UIKeyboardToolbar : UIToolbar
{
private bool _NextButtonVisible;
private bool _PreviousButtonVisible;
private UIBarButtonItem _NextButton;
private UIBarButtonItem _PrevButton;
private UIBarButtonItem _Spacer;
private UIBarButtonItem _DoneButton;
private IFocusable _FocusableElement;
public bool NextButtonVisible
{
get { return _NextButtonVisible; }
set
{
if (_NextButtonVisible != value)
{
_NextButtonVisible = value;
SetNeedsLayout();
}
}
}
public bool PreviousButtonVisible
{
get { return _PreviousButtonVisible; }
set
{
if (_PreviousButtonVisible != value)
{
_PreviousButtonVisible = value;
SetNeedsLayout();
}
}
}
public UIKeyboardToolbar(IFocusable focusableElement) : base(new RectangleF(0, 0, 320, 44))
{
_FocusableElement = focusableElement;
PreviousButtonVisible = true;
NextButtonVisible = true;
_Spacer = new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace);
_DoneButton = new UIBarButtonItem(UIBarButtonSystemItem.Done, DismissKeyboard);
if (_FocusableElement != null && _FocusableElement.Entry != null && _FocusableElement.Entry.InputAccessoryView == null)
{
TintColor = ((IElement)_FocusableElement).Theme.BarTintColor;
Translucent = true;
}
}
public UIKeyboardToolbar(NSCoder coder) : base(coder)
{
}
public UIKeyboardToolbar(NSObjectFlag t) : base(t)
{
}
public UIKeyboardToolbar(IntPtr handle) : base(handle)
{
}
public UIKeyboardToolbar(RectangleF frame) : base(frame)
{
}
public override void LayoutSubviews()
{
base.LayoutSubviews();
if (PreviousButtonVisible)
_PrevButton = new UIBarButtonItem("Previous", UIBarButtonItemStyle.Bordered, PreviousField);
if (NextButtonVisible)
_NextButton = new UIBarButtonItem("Next", UIBarButtonItemStyle.Bordered, NextField);
UIBarButtonItem[] buttons = { _PrevButton, _NextButton, _Spacer, _DoneButton };
Items = buttons.Where((b)=>b != null).ToArray();
}
public void PreviousField(object sender, EventArgs e)
{
_FocusableElement.MovePrev();
}
public void NextField(object sender, EventArgs e)
{
_FocusableElement.MoveNext();
}
public void DismissKeyboard(object sender, EventArgs e)
{
_FocusableElement.Entry.ResignFirstResponder();
}
}
}
//
// UICustomTextField.cs
//
// Author:
// Robert Kozak (rkozak@gmail.com / Twitter:@robertkozak)
//
// Copyright 2011, Nowcom Corporation.
//
// Code licensed under the MIT X11 license
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
namespace MonoMobile.MVVM
{
using System;
using System.Drawing;
using MonoTouch.CoreGraphics;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
[Preserve(AllMembers = true)]
public class UICustomTextField : UITextField
{
public UIColor PlaceholderColor { get; set; }
public UITextAlignment PlaceholderAlignment { get; set; }
public UICustomTextField(): base() {}
public UICustomTextField(NSCoder coder): base(coder) {}
public UICustomTextField(NSObjectFlag t): base(t) {}
public UICustomTextField(IntPtr handle): base (handle) {}
public UICustomTextField(RectangleF frame): base(frame) {}
public override void DrawPlaceholder(RectangleF rect)
{
if (PlaceholderColor == null)
PlaceholderColor = UIColor.FromWhiteAlpha(0.7f, 1.0f);
PlaceholderColor.SetColor();
DrawString(Placeholder, rect, UIFont.SystemFontOfSize(UIFont.LabelFontSize), UILineBreakMode.Clip, PlaceholderAlignment);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment