Пример #1
0
		public MainContentPage()
		{
			var extendedEntry = new ExtendedEntry();

			var extended1 = new ExtendedEntry()
			{
				PlaceholderTextColor = Color.Fuchsia,
				Placeholder = "Hello from placeholder"
			};

			Content = new StackLayout
			{
				VerticalOptions = LayoutOptions.Center,
				Children =
				{
					new Label
					{
						XAlign = TextAlignment.Center,
						Text = "Welcome to Xamarin Forms!"
					},
					extendedEntry,
					extended1
				}
			};
		}
		/// <summary>
		/// Sets the border.
		/// </summary>
		/// <param name="view">The view.</param>
		private void SetBorder(ExtendedEntry view)
		{
			if (view.IsPassword && _thisPasswordBox != null)
			{
				_thisPasswordBox.BorderThickness = view.HasBorder ? new System.Windows.Thickness(2) : new System.Windows.Thickness(0);
			}
			else if (!view.IsPassword && _thisPhoneTextBox != null)
			{
				_thisPhoneTextBox.BorderThickness = view.HasBorder ? new System.Windows.Thickness(2) : new System.Windows.Thickness(0);
			}
		}
		/// <summary>
		/// Sets the MaxLength characteres.
		/// </summary>
		/// <param name="view">The view.</param>
		private void SetMaxLength(ExtendedEntry view)
		{
			Control.SetFilters(new IInputFilter[] { new global::Android.Text.InputFilterLengthFilter(view.MaxLength) });
		}
		/// <summary>
		/// Sets the color of the placeholder text.
		/// </summary>
		/// <param name="view">The view.</param>
		private void SetPlaceholderTextColor(ExtendedEntry view)
		{
			if (view.PlaceholderTextColor != Color.Default)
				Control.SetHintTextColor(view.PlaceholderTextColor.ToAndroid());
		}
		/// <summary>
		/// Sets the font.
		/// </summary>
		/// <param name="view">The view.</param>
		private void SetFont(ExtendedEntry view)
		{
			if (view.Font != Font.Default)
			{
				Control.TextSize = view.Font.ToScaledPixel();
				Control.Typeface = view.Font.ToExtendedTypeface(Context);
			}
		}
		/// <summary>
		/// Sets the text alignment.
		/// </summary>
		/// <param name="view">The view.</param>
		private void SetTextAlignment(ExtendedEntry view)
		{
			switch (view.XAlign)
			{
				case Xamarin.Forms.TextAlignment.Center:
					Control.Gravity = GravityFlags.CenterHorizontal;
					break;
				case Xamarin.Forms.TextAlignment.End:
					Control.Gravity = GravityFlags.End;
					break;
				case Xamarin.Forms.TextAlignment.Start:
					Control.Gravity = GravityFlags.Start;
					break;
			}
		}
		/// <summary>
		/// Sets the border.
		/// </summary>
		/// <param name="view">The view.</param>
		private void SetBorder(ExtendedEntry view)
		{
			//NotCurrentlySupported: HasBorder peroperty not suported on Android
		}
		/// <summary>
		/// Sets the text alignment.
		/// </summary>
		/// <param name="view">The view.</param>
		private void SetTextAlignment(ExtendedEntry view)
		{
			if (view.IsPassword && _thisPasswordBox != null)
			{
				switch (view.XAlign)
				{
					//NotCurrentlySupported: Text alaignement not available on Windows Phone for Password Entry
				}
			}
			else if (!view.IsPassword && _thisPhoneTextBox != null)
			{
				switch (view.XAlign)
				{
					case TextAlignment.Center:
						_thisPhoneTextBox.TextAlignment = System.Windows.TextAlignment.Center;
						break;
					case TextAlignment.End:
						_thisPhoneTextBox.TextAlignment = System.Windows.TextAlignment.Right;
						break;
					case TextAlignment.Start:
						_thisPhoneTextBox.TextAlignment = System.Windows.TextAlignment.Left;
						break;
				}
			}
		}
		/// <summary>
		/// Sets the MaxLength Characteres.
		/// </summary>
		/// <param name="view">The view.</param>
		private void SetMaxLength(ExtendedEntry view)
		{
			_thisPhoneTextBox.MaxLength = view.MaxLength;
		}
		/// <summary>
		/// Sets the color of the placeholder text.
		/// </summary>
		/// <param name="view">The view.</param>
		private void SetPlaceholderTextColor(ExtendedEntry view)
		{
			//the EntryRenderer renders two child controls. A PhoneTextBox or PasswordBox
			// depending on the IsPassword property of the Xamarin form control

			if (view.IsPassword)
			{
				//NotCurrentlySupported: Placeholder text color is not supported on Windows Phone Password control
			}
			else
			{
				if (view.PlaceholderTextColor != Color.Default && _thisPhoneTextBox != null)
				{
					var hintStyle = new System.Windows.Style(typeof(System.Windows.Controls.ContentControl));
					hintStyle.Setters.Add(
						new System.Windows.Setter(
							System.Windows.Controls.Control.ForegroundProperty,
							view.PlaceholderTextColor.ToBrush())
						);
					_thisPhoneTextBox.HintStyle = hintStyle;
				}
			}
		}
		/// <summary>
		/// Sets the font.
		/// </summary>
		/// <param name="view">The view.</param>
		private void SetFont(ExtendedEntry view)
		{
			if (view.Font != Font.Default)
				if (view.IsPassword)
				{
					if (_thisPasswordBox != null)
					{
						_thisPasswordBox.FontSize = view.Font.GetHeight();
					}
				}
				else
				{
					if (_thisPhoneTextBox != null)
					{
						_thisPhoneTextBox.FontSize = view.Font.GetHeight();
					}
				}
		}
		/// <summary>
		/// Sets the color of the placeholder text.
		/// </summary>
		/// <param name="view">The view.</param>
		void SetPlaceholderTextColor(ExtendedEntry view)
		{
			/*
UIColor *color = [UIColor lightTextColor];
YOURTEXTFIELD.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"PlaceHolder Text" attributes:@{NSForegroundColorAttributeName: color}];
			*/
			if (string.IsNullOrEmpty(view.Placeholder) == false && view.PlaceholderTextColor != Color.Default)
			{
				NSAttributedString placeholderString = new NSAttributedString(view.Placeholder, new UIStringAttributes() { ForegroundColor = view.PlaceholderTextColor.ToUIColor() });
				Control.AttributedPlaceholder = placeholderString;
			}
		}
		/// <summary>
		/// Sets the maxLength.
		/// </summary>
		/// <param name="view">The view.</param>
		private void SetMaxLength(ExtendedEntry view)
		{
			Control.ShouldChangeCharacters = (textField, range, replacementString) =>
			{
				var newLength = textField.Text.Length + replacementString.Length - range.Length;
				return newLength <= view.MaxLength;
			};
		}
		/// <summary>
		/// Sets the border.
		/// </summary>
		/// <param name="view">The view.</param>
		private void SetBorder(ExtendedEntry view)
		{
			Control.BorderStyle = view.HasBorder ? UITextBorderStyle.RoundedRect : UITextBorderStyle.None;
		}
		/// <summary>
		/// Sets the font.
		/// </summary>
		/// <param name="view">The view.</param>
		private void SetFont(ExtendedEntry view)
		{
			UIFont uiFont;
			if (view.Font != Font.Default && (uiFont = view.Font.ToUIFont()) != null)
				Control.Font = uiFont;
			else if (view.Font == Font.Default)
				Control.Font = UIFont.SystemFontOfSize(17f);
		}
		/// <summary>
		/// Sets the text alignment.
		/// </summary>
		/// <param name="view">The view.</param>
		private void SetTextAlignment(ExtendedEntry view)
		{
			switch (view.XAlign)
			{
				case TextAlignment.Center:
					Control.TextAlignment = UITextAlignment.Center;
					break;
				case TextAlignment.End:
					Control.TextAlignment = UITextAlignment.Right;
					break;
				case TextAlignment.Start:
					Control.TextAlignment = UITextAlignment.Left;
					break;
			}
		}