Inheritance: UITextField
示例#1
0
        protected virtual void PrepareEntry(UITableView tableview)
        {
            SizeF size = _computeEntryPosition(tableview);

            _entry          = new CustomTextField(new RectangleF(size.Width + 10, (ContentView.Bounds.Height - size.Height) / 2 - 10, 320 - size.Width - 20, size.Height + 20));
            _delegate       = new CustomTextFieldDelegate();
            _entry.Delegate = _delegate;

            _entry.VerticalAlignment = UIControlContentVerticalAlignment.Center;

            TextLabel.BackgroundColor = UIColor.Clear;
            _entry.AutoresizingMask   = UIViewAutoresizing.FlexibleWidth |
                                        UIViewAutoresizing.FlexibleLeftMargin;

            _entry.MaxCharacters = 5;

            _entry.Started += delegate {
                var position = tableview.IndexPathForCell(this);
                tableview.SelectRow(position, false, UITableViewScrollPosition.None);
            };

            _entry.ValueChanged += delegate {
                if (_element != null)
                {
                    _element.Value = _entry.Text;
                }
            };
            _entry.EnablesReturnKeyAutomatically = true;
            _entry.Ended += (object sender, EventArgs e) => {
                if (_element != null)
                {
                    _element.Value = _entry.Text;

                    if (_element.OnValueChanged != null)
                    {
                        _element.OnValueChanged(_element);
                    }
                }

                var position = tableview.IndexPathForCell(this);
                if (tableview.IndexPathForSelectedRow != null && position != null && position.Compare(tableview.IndexPathForSelectedRow) == 0)
                {
                    tableview.DeselectRow(position, false);
                }
            };
            _entry.ShouldChangeCharacters = (textField, range, replacement) =>
            {
                if (_element.MaxLength < 0)
                {
                    return(true);
                }
                if (_element.MaxLength == 0)
                {
                    return(false);
                }
                using (NSString original = new NSString(textField.Text))
                {
                    var replace = original.Replace(range, new NSString(replacement));
                    if (replace.Length > _element.MaxLength)
                    {
                        return(false);
                    }
                }
                return(true);
            };

            _entry.AddTarget((object o, EventArgs r) => {
                if (_element != null)
                {
                    _element.Value = _entry.Text;
                }
            }, UIControlEvent.EditingChanged);

            _entry.ShouldReturn += delegate {
                Element elementToFocusOn = null;

                foreach (var c in ((Section)_element.Parent).Elements)
                {
                    if (c == _element)
                    {
                        elementToFocusOn = c;
                    }
                    else if (elementToFocusOn != null && c is EntryElement)
                    {
                        elementToFocusOn = c as EntryElement;
                    }
                }
                if (elementToFocusOn != _element && elementToFocusOn != null)
                {
                    var cell = tableview.CellAt(elementToFocusOn.GetIndexPath());
                    cell.BecomeFirstResponder();
                }
                else
                {
                    _entry.ResignFirstResponder();
                }

                if (_entry.ReturnKeyType == UIReturnKeyType.Go)
                {
                    _element.FireGo(this, EventArgs.Empty);
                }

                return(true);
            };
            _entry.Started += delegate {
                EntryElement self       = null;
                var          returnType = _element.ReturnKeyType;

                if (returnType != UIReturnKeyType.Default)
                {
                    foreach (var e in (_element.Parent as Section).Elements)
                    {
                        if (e == _element)
                        {
                            self = _element;
                        }
                        else if (self != null && e is EntryElement)
                        {
                            returnType = UIReturnKeyType.Next;
                        }
                    }
                }
                _entry.ReturnKeyType = returnType;
            };

            ContentView.AddSubview(_entry);
        }
		protected virtual void PrepareEntry(UITableView tableview){
			SizeF size = _computeEntryPosition(tableview);
			
			_entry = new CustomTextField (new RectangleF (size.Width+10, (ContentView.Bounds.Height-size.Height)/2-10, 320-size.Width-20, size.Height+20));
			_delegate = new CustomTextFieldDelegate ();
			_entry.Delegate = _delegate;

			_entry.VerticalAlignment = UIControlContentVerticalAlignment.Center;

			TextLabel.BackgroundColor = UIColor.Clear;
			_entry.AutoresizingMask = UIViewAutoresizing.FlexibleWidth |
				UIViewAutoresizing.FlexibleLeftMargin;

			_entry.MaxCharacters = 5;

			_entry.Started += delegate {
				var position = tableview.IndexPathForCell(this);
				tableview.SelectRow(position, false, UITableViewScrollPosition.None);
			};

			_entry.ValueChanged += delegate {
				if (_element != null) {
					_element.Value = _entry.Text;
				}
			};
			_entry.EnablesReturnKeyAutomatically = true;
			_entry.Ended += (object sender, EventArgs e) => {
				if (_element != null) {
					_element.Value = _entry.Text;
					
					if (_element.OnValueChanged!=null)
						_element.OnValueChanged(_element);
				}
				
				var position = tableview.IndexPathForCell(this);
				if (tableview.IndexPathForSelectedRow!=null && position!=null && position.Compare(tableview.IndexPathForSelectedRow)==0){
					tableview.DeselectRow(position, false);
				}

			};
			_entry.ShouldChangeCharacters = (textField, range, replacement) => 
			{
				if (_element.MaxLength<0) return true;
				if (_element.MaxLength==0) return false;
				using (NSString original = new NSString(textField.Text))
				{
					var replace = original.Replace(range, new NSString(replacement));
					if (replace.Length>_element.MaxLength)
						return false;
				}
				return true;
			};

			_entry.AddTarget((object o, EventArgs r)=>{
				if (_element != null)
					_element.Value = _entry.Text;
				}, UIControlEvent.EditingChanged);
				
			_entry.ShouldReturn += delegate {
				Element elementToFocusOn = null;
				
				foreach (var c in ((Section)_element.Parent).Elements){
					if (c == _element)
						elementToFocusOn = c;
					else if (elementToFocusOn != null && c is EntryElement)
						elementToFocusOn = c as EntryElement;
				}
				if (elementToFocusOn != _element && elementToFocusOn!=null) {
					var cell = tableview.CellAt(elementToFocusOn.GetIndexPath());
					cell.BecomeFirstResponder();
				}
				else 
					_entry.ResignFirstResponder();

                if (_entry.ReturnKeyType == UIReturnKeyType.Go) {
                    _element.FireGo(this, EventArgs.Empty);
                }

				return true;
			};
			_entry.Started += delegate {
				EntryElement self = null;
				var returnType = _element.ReturnKeyType;

                if (returnType != UIReturnKeyType.Default) {
                    foreach (var e in (_element.Parent as Section).Elements){
                        if (e == _element)
                            self = _element;
                        else if (self != null && e is EntryElement)
                            returnType = UIReturnKeyType.Next;
                    }
                }
                _entry.ReturnKeyType = returnType;
			};
				
			ContentView.AddSubview (_entry);
		}