xamarin.ios - Xamarin Forms 向 iOS 编辑器添加占位符

标签 xamarin.ios xamarin.forms

如何在 Xamarin Forms for iOS 中向编辑器添加占位符?

我尝试通过自定义渲染器将其添加为 Control.Layer,但找不到与之相关的属性。

请帮忙。

最佳答案

试试下面的代码:

PCL:

using System;
using Xamarin.Forms;

namespace ABC.CustomViews
{
    public class PlaceholderEditor : Editor
    {
        public static readonly BindableProperty PlaceholderProperty =
            BindableProperty.Create<PlaceholderEditor, string>(view => view.Placeholder, String.Empty);

        public PlaceholderEditor()
        {
        }

        public string Placeholder
        {
            get
            {
                return (string)GetValue(PlaceholderProperty);
            }

            set
            {
                SetValue(PlaceholderProperty, value);
            }
        }
    }
}

iOS(客户渲染器):

using UIKit;
using ABC.CustomViews;
using ABC.iOS.CustomRenderer;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly: ExportRenderer(typeof(PlaceholderEditor), typeof(PlaceholderEditorRenderer))]
namespace ABC.iOS.CustomRenderer
{
    public class PlaceholderEditorRenderer : EditorRenderer
    {
        private string Placeholder { get; set; }

        protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
        {
            base.OnElementChanged(e);
            var element = this.Element as PlaceholderEditor;

            if (Control != null && element != null)
            {
                Placeholder = element.Placeholder;
                Control.TextColor = UIColor.LightGray;
                Control.Text = Placeholder;

                Control.ShouldBeginEditing += (UITextView textView) =>
                {
                    if (textView.Text == Placeholder)
                    {
                        textView.Text = "";
                        textView.TextColor = UIColor.Black; // Text Color
                    }

                    return true;
                };

                Control.ShouldEndEditing += (UITextView textView) =>
                {
                    if (textView.Text == "")
                    {
                        textView.Text = Placeholder;
                        textView.TextColor = UIColor.LightGray; // Placeholder Color
                    }

                    return true;
                };
            }
        }
    }
}

用法:

  _replyEditor = new PlaceholderEditor
  {
        Placeholder = "Placeholder Text"
  };

关于xamarin.ios - Xamarin Forms 向 iOS 编辑器添加占位符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41449275/

相关文章:

c# - Xamarin.iOS 状态栏在更改其文本颜色时消失

c# - 输入文本通过键盘消失

xamarin.forms - OnElementChanged(ElementChangedEventArgs<T> 什么时候触发?

xamarin - 在 Xamarin Forms 应用程序中,如何在 Android 中使用 TalkBack 时将焦点放在标签和图像等非交互式元素上

c# - 如何在 ios 上的 Xamarin.Forms CollectionView 中关闭弹跳效果

c# - MonoTouch 到 Objective-C

xamarin.ios - 文件共享在 Xamarin iOS 中不起作用

iphone - 如何使用 MonoTouch 在 iPhone 上播放视频?

ios - TPL 任务运行时如何显示 iOS 网络指示器?

c# - 如何删除或隐藏特定页面中的工具栏项错误 : System. IndexOutOfRangeException:索引超出数组范围