File: TextFieldAlertView.cs

package info (click to toggle)
monogame 2.5.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 6,060 kB
  • ctags: 10,325
  • sloc: cs: 65,996; xml: 591; makefile: 22; ansic: 8
file content (112 lines) | stat: -rw-r--r-- 3,141 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System.Drawing;

using MonoTouch.Foundation;
using MonoTouch.UIKit;

namespace Microsoft.Xna.Framework.GamerServices
{
	public class TextFieldAlertView : UIAlertView
	{
		private UITextField _tf = null;
		private bool _secureTextEntry;
		
		private string _initEditValue;
		private string _placeHolderValue;
		
		public TextFieldAlertView() : this(false) {}
		
		public TextFieldAlertView(bool secureTextEntry, string title, string message, UIAlertViewDelegate alertViewDelegate, string cancelBtnTitle, params string[] otherButtons)
			: base(title, message, alertViewDelegate, cancelBtnTitle, otherButtons)
		{
			InitializeControl(secureTextEntry);
		}
		
		public TextFieldAlertView(bool secureTextEntry)
		{	
			InitializeControl(secureTextEntry);
		}
		
		public TextFieldAlertView(bool secureTextEntry, string initEditValue, string placeHolderValue )
		{	
			InitializeControl(secureTextEntry);
			_initEditValue = initEditValue;
			_placeHolderValue = placeHolderValue;
		}
		
		private void InitializeControl(bool secureTextEntry)
		{
			_secureTextEntry = secureTextEntry;
			this.AddButton("Cancel");
			this.AddButton("Ok");
			
			// build out the text field
			_tf = ComposeTextFieldControl(_secureTextEntry);
			
			// add the text field to the alert view
			this.AddSubview(_tf);
		}
		
		public string EnteredText 
		{ 
			get 
			{ 
				return _tf.Text; 
			} 
		}
		
		public override void LayoutSubviews ()
		{
			// layout the stock UIAlertView control
			base.LayoutSubviews ();
			
			// We can only force it to become a First Responder after it has been added to the MainView.
			_tf.BecomeFirstResponder();
		}

		private UITextField ComposeTextFieldControl(bool secureTextEntry)
		{
			UITextField textField = new UITextField (new System.Drawing.RectangleF(12f, 45f, 260f, 25f));
			textField.BackgroundColor = UIColor.White;
			textField.UserInteractionEnabled = true;
			textField.AutocorrectionType = UITextAutocorrectionType.No;
			textField.AutocapitalizationType = UITextAutocapitalizationType.None;
			textField.ReturnKeyType = UIReturnKeyType.Done;
			textField.SecureTextEntry = secureTextEntry;
			
			textField.Text = _initEditValue;
			textField.Placeholder = _placeHolderValue;
			textField.BecomeFirstResponder();
			return textField;
		}
		
		public override void Show ()
		{
			base.Show ();
			
			// shift the contents of the alert view around to accomodate the extra text field
			this.AdjustControlSize();
		}
		
		private void AdjustControlSize()
		{
			float tfExtH = _tf.Frame.Size.Height + 16.0f;
			
			RectangleF frame = new RectangleF(this.Frame.X, 
			                                  this.Frame.Y - tfExtH/2,
			                                  this.Frame.Size.Width,
			                                  this.Frame.Size.Height + tfExtH);
			this.Frame = frame;
			
			foreach(var view in this.Subviews)
			{
				if(view is UIControl)
				{
					view.Frame = new RectangleF(view.Frame.X, 
					                            view.Frame.Y + tfExtH,
					                            view.Frame.Size.Width, 
					                            view.Frame.Size.Height);
				}
			}
		}
	}
}