Xamarin 窗体条目框不会在 iOS 中水平滚动



如果我的 Xamarin Forms 项目中有一个Entry,并且我一直在上面键入,直到它离开可查看页面的末尾,文本在 iOS 中不会像在 Android 上那样水平滚动 - 有没有办法让它这样做?

我尝试用编辑器替换它,但它没有我需要的占位符。

条目在

不支持滚动的 iOS 上呈现为 UITextField。编辑器呈现为支持滚动的 UITextView。

一种选择是创建包含占位符的自定义编辑器。

我有这方面的代码。它不漂亮,但它有效。

下面是自定义编辑器代码,包括占位符文本和颜色的新可绑定属性。

   public class CustomEditor : Editor
    {
        public static readonly BindableProperty PlaceholderProperty =
            BindableProperty.Create(nameof(Placeholder), typeof(string), typeof(CustomEditor), default(string));
        public string Placeholder
        {
            get { return (string)GetValue(PlaceholderProperty); }
            set { SetValue(PlaceholderProperty, value); }
        }
        public static readonly BindableProperty PlaceholderColorProperty =
            BindableProperty.Create(nameof(PlaceholderColor), typeof(Color), typeof(CustomEditor), default(Color));
        public Color PlaceholderColor
        {
            get { return (Color)GetValue(PlaceholderColorProperty); }
            set { SetValue(PlaceholderColorProperty, value); }
        }
        public static readonly BindableProperty DisabledColorProperty =
            BindableProperty.Create(nameof(DisabledColor), typeof(Color), typeof(CustomEditor), default(Color));
        public Color DisabledColor
        {
            get { return (Color)GetValue(DisabledColorProperty); }
            set { SetValue(DisabledColorProperty, value); }
        }
    }

这是 iOS 自定义渲染器

[assembly: ExportRenderer(typeof(CustomEditor), typeof(CustomEditorRenderer))]
namespace Test.iOS.CustomRenderers
{
    public class CustomEditorRenderer : EditorRenderer
    {
        private UILabel PlaceholderLabel { get; set; }
        protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
        {
            base.OnElementChanged(e);
            if (Control == null) return;
            Control.Layer.BorderWidth = 0.5f;
            Control.Layer.CornerRadius = 8;
            Control.Layer.BorderColor = Color.FromHex("CDCDCD").ToCGColor();
            if (PlaceholderLabel != null) return;
            var element = Element as CustomEditor;
            PlaceholderLabel = new UILabel
            {
                Text = element?.Placeholder,
                TextColor = element?.PlaceholderColor.ToUIColor(),
                BackgroundColor = UIColor.Clear
            };
            var edgeInsets = Control.TextContainerInset;
            var lineFragmentPadding = Control.TextContainer.LineFragmentPadding;
            Control.AddSubview(PlaceholderLabel);
            var vConstraints = NSLayoutConstraint.FromVisualFormat(
                "V:|-" + edgeInsets.Top + "-[PlaceholderLabel]-" + edgeInsets.Bottom + "-|", 0, new NSDictionary(),
                NSDictionary.FromObjectsAndKeys(
                    new NSObject[] { PlaceholderLabel }, new NSObject[] { new NSString("PlaceholderLabel") })
            );
            var hConstraints = NSLayoutConstraint.FromVisualFormat(
                "H:|-" + lineFragmentPadding + "-[PlaceholderLabel]-" + lineFragmentPadding + "-|",
                0, new NSDictionary(),
                NSDictionary.FromObjectsAndKeys(
                    new NSObject[] { PlaceholderLabel }, new NSObject[] { new NSString("PlaceholderLabel") })
            );
            PlaceholderLabel.TranslatesAutoresizingMaskIntoConstraints = false;
            Control.AddConstraints(hConstraints);
            Control.AddConstraints(vConstraints);
            PlaceholderLabel.Hidden = Element.Text.NotEmpty();
        }
        protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);
            if (e.PropertyName == "Text")
            {
                PlaceholderLabel.Hidden = Control.Text.NotEmpty();
            }
            if (e.PropertyName == VisualElement.IsEnabledProperty.PropertyName)
            {
                var element = Element as CustomEditor;
                SetTextColor(element);
            }
        }
        private void SetTextColor(CustomEditor element)
        {
            Control.TextColor = element.IsEnabled ? element.TextColor.ToUIColor() : element.DisabledColor.ToUIColor();
        }
    }
}

最新更新