Created
May 3, 2011 07:29
-
-
Save nicwise/952949 to your computer and use it in GitHub Desktop.
Multiline Edit Element for MonoTouch.Dialog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using MonoTouch.Dialog; | |
using MonoTouch.UIKit; | |
using MonoTouch.Foundation; | |
using System.Drawing; | |
using MonoTouch.CoreGraphics; | |
namespace DeadDrop | |
{ | |
public class MultilineEditElement : StringElement | |
{ | |
static NSString skey = new NSString("multilineeditelement"); | |
public MultilineEditElement(string caption, string val) : base(caption, val) | |
{ | |
} | |
class MyViewController : UIViewController { | |
MultilineEditElement container; | |
UITextView editbox; | |
private const float PortratKeyboardHeight = 216; | |
private const float LandscapeKeyboardHeight = 162; | |
private RootElement rootElement; | |
public MyViewController (MultilineEditElement container, RootElement rootElement) | |
{ | |
this.container = container; | |
this.rootElement = rootElement; | |
NavigationItem.Title = container.Caption; | |
editbox = new UITextView(); | |
editbox.Text = container.Value; | |
editbox.Font = UIFont.SystemFontOfSize(17f); | |
this.View.Add(editbox); | |
} | |
public override void ViewDidAppear (bool animated) | |
{ | |
base.ViewDidAppear (animated); | |
SetViewOrientationAndSize(); | |
editbox.BecomeFirstResponder(); | |
} | |
public override void ViewWillDisappear (bool animated) | |
{ | |
base.ViewWillDisappear (animated); | |
container.Value = editbox.Text; | |
rootElement.Reload(container, UITableViewRowAnimation.None); | |
} | |
public override void DidRotate (UIInterfaceOrientation fromInterfaceOrientation) | |
{ | |
base.DidRotate (fromInterfaceOrientation); | |
SetViewOrientationAndSize(); | |
} | |
private void SetViewOrientationAndSize() | |
{ | |
float keyboardHeight = 0; | |
switch (UIDevice.CurrentDevice.Orientation) | |
{ | |
case UIDeviceOrientation.LandscapeLeft: | |
case UIDeviceOrientation.LandscapeRight: | |
keyboardHeight = LandscapeKeyboardHeight; | |
break; | |
case UIDeviceOrientation.Portrait: | |
case UIDeviceOrientation.PortraitUpsideDown: | |
keyboardHeight = PortratKeyboardHeight; | |
break; | |
} | |
//Console.WriteLine("View: X: {0} Y: {1} W: {2} H:{3}", View.Frame.X, View.Frame.Y, View.Frame.Width, View.Frame.Height); | |
RectangleF viewRect = new RectangleF(View.Frame.X, View.Frame.Y, View.Frame.Width, View.Frame.Height - keyboardHeight + 50); | |
// 50 == we have a tab bar at the bottom!! :( | |
editbox.Frame = viewRect; | |
} | |
public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation) | |
{ | |
return true; | |
} | |
} | |
public override UITableViewCell GetCell (UITableView tv) | |
{ | |
var cell = tv.DequeueReusableCell (skey); | |
if (cell == null){ | |
cell = new UITableViewCell (Value == null ? UITableViewCellStyle.Default : UITableViewCellStyle.Value1, skey); | |
cell.SelectionStyle = UITableViewCellSelectionStyle.Blue; | |
} | |
cell.Accessory = UITableViewCellAccessory.None; | |
cell.TextLabel.Text = Caption; | |
cell.TextLabel.TextAlignment = Alignment; | |
// The check is needed because the cell might have been recycled. | |
if (cell.DetailTextLabel != null) | |
{ | |
cell.DetailTextLabel.Text = Value == null ? "" : Value; | |
cell.DetailTextLabel.AdjustsFontSizeToFitWidth = true; | |
cell.DetailTextLabel.MinimumFontSize = 13.0f; | |
} | |
return cell; | |
} | |
public override void Selected (DialogViewController dvc, UITableView tableView, NSIndexPath path) | |
{ | |
dvc.ActivateController (new MyViewController (this, dvc.Root)); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, how would you create the Changed event?
I need to have something like
MultilineEditElement myElement = new MultilineEditElement ("Example", "");
myElement.Changed += myChangedEvent;