Created
April 2, 2013 12:47
-
-
Save nicwise/5291940 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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; | |
NavigationItem.RightBarButtonItem = new UIBarButtonItem(UIBarButtonSystemItem.Done, Done); | |
editbox = new UITextView(); | |
editbox.Text = container.Value; | |
editbox.Font = UIFont.SystemFontOfSize(17f); | |
this.View.Add(editbox); | |
} | |
public void Done(object sender, EventArgs e) | |
{ | |
NavigationController.PopViewControllerAnimated(true); | |
} | |
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