protected override void OnElementChanged(ElementChangedEventArgs <Xamarin.Forms.Entry> e)
        {
            base.OnElementChanged(e);

            if (e.NewElement != null)
            {
                formControl = (Element as CoreUnderlineEntry);

                var editText = (EditText)Control;
                editText.EditorAction += (obj, act) =>
                {
                    formControl?.NextFocus?.Invoke();
                };
                editText.Touch += (a, aa) =>
                {
                    aa.Handled = false;
                    var w  = editText.Width;
                    var wl = editText.CompoundPaddingLeft;
                    var wr = w - editText.CompoundPaddingRight;
                    var x  = aa.Event.GetX();
                    if (wr < x && aa.Event.Action == Views.MotionEventActions.Down)
                    {
                        if (formControl.IsPassword &&
                            formControl.PasswordRevealEnabled &&
                            !string.IsNullOrEmpty(formControl.PasswordRevealIcon) &&
                            !string.IsNullOrEmpty(formControl.PasswordHideIcon))
                        {
                            formControl.IsPassword = !formControl.IsPassword;
                            var rightDrawable = formControl.IsPassword == true?GetDrawable(formControl.PasswordRevealIcon) : GetDrawable(formControl.PasswordHideIcon);

                            editText.SetCompoundDrawablesWithIntrinsicBounds(GetDrawable(formControl.Icon)?.Target as Drawable, null, rightDrawable?.Target as Drawable, null);
                            editText.CompoundDrawablePadding = 20;
                        }
                    }
                };

                if (formControl != null &&
                    formControl.IsPassword &&
                    !string.IsNullOrEmpty(formControl.PasswordRevealIcon) &&
                    !string.IsNullOrEmpty(formControl.PasswordHideIcon) &&
                    formControl.PasswordRevealEnabled)
                {
                    var size          = editText.TextSize;
                    var rightDrawable = formControl.IsPassword == true?GetDrawable(formControl.PasswordRevealIcon) : null;

                    editText.SetCompoundDrawablesWithIntrinsicBounds(GetDrawable(formControl.Icon)?.Target as Drawable, null, rightDrawable?.Target as Drawable, null);
                    editText.CompoundDrawablePadding = 20;
                    editText.Gravity = Views.GravityFlags.Bottom;
                }
                if (!formControl.PasswordRevealEnabled && !string.IsNullOrEmpty(formControl.ClearEntryIcon) && formControl.ClearEntryEnabled)
                {
                    var iconResource = formControl.ClearEntryIcon.Replace(".png", string.Empty).Replace(".jpg", string.Empty);
                    var clearIcon    = GetResourceIdByName(iconResource);
                    editText.SetCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, clearIcon, 0);

                    editText.SetOnTouchListener(new OnDrawableTouchListener());
                }
                if (formControl != null && formControl.EntryColor != null)
                {
                    editText.Background.Mutate().SetColorFilter(formControl.EntryColor.ToAndroid(), Graphics.PorterDuff.Mode.SrcAtop);
                }

                if ((Control != null) & (e.NewElement != null))
                {
                    var entryExt = (e.NewElement as CoreUnderlineEntry);
                    Control.ImeOptions = entryExt.ReturnKeyType.GetValueFromDescription();
                    Control.SetImeActionLabel(entryExt.ReturnKeyType.ToString(), Control.ImeOptions);
                }

                if (!string.IsNullOrEmpty(e.NewElement?.StyleId) && Control != null)
                {
                    var fileName = e.NewElement.StyleId + ".ttf";
                    if (Resources.Assets.List("").Contains(fileName))
                    {
                        var font = Graphics.Typeface.CreateFromAsset(context.ApplicationContext.Assets, fileName);
                        Control.Typeface = font;
                    }
                }
            }
        }
Ejemplo n.º 2
0
        protected override void OnElementChanged(ElementChangedEventArgs <Entry> e)
        {
            base.OnElementChanged(e);

            if (Control != null && Element != null)
            {
                Control.BorderStyle = UITextBorderStyle.None;

                formControl = (Element as CoreUnderlineEntry);
                if ((Control != null) & (formControl != null))
                {
                    var returnType = formControl.ReturnKeyType;

                    if (this.Element.Keyboard == Keyboard.Numeric || this.Element.Keyboard == Keyboard.Telephone)
                    {
                        this.AddAccessoryButton(returnType);
                    }
                    else
                    {
                        Control.ReturnKeyType = returnType.GetValueFromDescription();
                    }
                }

                ((UITextField)Control).ShouldReturn = (textField) =>
                {
                    formControl?.NextFocus?.Invoke();
                    return(true);
                };

                controlColor = formControl.EntryColor.ToCGColor();

                var    ctrl                      = (UITextField)Control;
                var    fontSize                  = ctrl.Font.PointSize;
                var    iconHeight                = fontSize + 9; // UIScreen.MainScreen.Bounds.Height * .03;
                var    passwordIconAdder         = 2;
                var    passwordIconPaddingHeight = 2;
                var    passwordIconHeight        = iconHeight + passwordIconAdder;
                var    passwordIconPaddingWidth  = 10;
                var    iconPadding               = passwordIconAdder / 2 + passwordIconPaddingHeight;
                nfloat iconFrameHeight           = 0;
                nfloat iconPasswordFrameHeight   = 0;

                if (formControl.Icon != null)
                {
                    if (formControl.Icon.IndexOf(".png", StringComparison.InvariantCultureIgnoreCase) == -1)
                    {
                        formControl.Icon = formControl.Icon + ".png";
                    }

                    imgView       = new UIImageView(new CGRect(0, 0, (iconHeight), (iconHeight)));
                    imgView.Image = new UIImage(formControl.Icon).ChangeImageColor(formControl.PlaceholderColor.ToUIColor());

                    Resize(imgView, fontSize, fontSize);

                    var paddingView = new UIView(new CGRect(0, 0, (iconHeight + 4), (iconHeight + iconPadding)));
                    iconFrameHeight = paddingView.Frame.Height;

                    paddingView.AddSubview(imgView);
                    ctrl.LeftViewMode = UITextFieldViewMode.Always;
                    ctrl.LeftView     = paddingView;
                }
                if (formControl.IsPassword &&
                    formControl.PasswordRevealEnabled &&
                    !string.IsNullOrEmpty(formControl.PasswordRevealIcon) &&
                    !string.IsNullOrEmpty(formControl.PasswordHideIcon)
                    )
                {
                    imgView       = new UIImageView(new CGRect(0, 0, (passwordIconHeight + 10), (passwordIconHeight + 10)));
                    imgView.Image = new UIImage(formControl.PasswordRevealIcon).ChangeImageColor(formControl.EntryColor.ToUIColor());

                    Resize(imgView, passwordIconHeight + passwordIconPaddingWidth, passwordIconHeight);

                    var paddingView = new UIView(new CGRect(0, 0, (passwordIconHeight + passwordIconPaddingWidth), (passwordIconHeight + passwordIconPaddingHeight + 10)));
                    iconPasswordFrameHeight = paddingView.Frame.Height;

                    var btn = new UIButton(paddingView.Frame);
                    btn.AddSubview(imgView);
                    btn.TouchUpInside += (ee, aa) =>
                    {
                        formControl.IsPassword = !formControl.IsPassword;
                        var fileName = formControl.IsPassword ? formControl.PasswordRevealIcon : formControl.PasswordHideIcon;
                        imgView.Image = new UIImage(fileName).ChangeImageColor(formControl.EntryColor.ToUIColor());
                    };
                    paddingView.AddSubview(btn);
                    ctrl.RightViewMode = UITextFieldViewMode.Always;
                    ctrl.RightView     = paddingView;
                    ctrl.RightView.UserInteractionEnabled = true;

                    Control.SpellCheckingType      = UITextSpellCheckingType.No;        // No Spellchecking
                    Control.AutocorrectionType     = UITextAutocorrectionType.No;       // No Autocorrection
                    Control.AutocapitalizationType = UITextAutocapitalizationType.None; // No Autocapitalization
                }

                if (!formControl.PasswordRevealEnabled && formControl.ClearEntryEnabled)
                {
                    ((UITextField)Control).ClearButtonMode = UITextFieldViewMode.WhileEditing;
                }

                if (iconFrameHeight != 0 && iconPasswordFrameHeight != 0)
                {
                    formControl.HeightRequest = iconFrameHeight > iconPasswordFrameHeight ? iconFrameHeight : iconPasswordFrameHeight;
                }
            }
        }