使用自定义呈现器的Xamarin表单编辑器上的占位符边距

ecbunoof  于 2022-12-07  发布在  其他
关注(0)|答案(2)|浏览(152)

我在Xamarin Forms项目的一个页面上有一个编辑器,我希望通过添加一点边距来修改占位符文本的位置。
我可以使用iOS Custom渲染器中的“TextContainerInset”为用户键入的文本(但不是占位符)添加边距。

protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
        {
           
                base.OnElementChanged(e);
                this.Control.InputAccessoryView = null;
                this.Control.Layer.CornerRadius = 10;
                this.Control.Layer.BorderColor = UIColor.LightGray.CGColor;
                this.Control.Layer.BorderWidth = (nfloat)0.5;
                this.Control.TextContainerInset = new UIEdgeInsets(15,15,15,15);    
          
        }

但是,此插入不适用于占位符位置。
有没有办法使用自定义渲染器移动占位符的位置?

wlp8pajw

wlp8pajw1#

您可以设置如下填充:

public class MyEntryRenderer : EntryRenderer
        {
            protected override void OnElementChanged (ElementChangedEventArgs<Entry> e)
            {
                base.OnElementChanged (e);
    
                if (Control != null) {
            Control.LeftView = new UIView(new CGRect(0,0,15,0));
            Control.LeftViewMode = UITextFieldViewMode.Always;
            Control.RightView = new UIView(new CGRect(0, 0, 15, 0));
            Control.RightViewMode = UITextFieldViewMode.Always;
                }
            }
}

编辑器的工作范围:

在框架中扭曲编辑器并设置填充,如下所示:

<StackLayout Margin="20">
    <Frame CornerRadius="10" BorderColor="Gray" HeightRequest="50" Padding="5,10,5,10" HasShadow="False">
        <Editor Placeholder="Enter the editor text" ></Editor>
    </Frame>
</StackLayout>
klsxnrf1

klsxnrf12#

基础UITextView不实现Placeholder。但是,如果查看从UITextView派生的EditorRenderer代码,就会发现Xamarin已在此处实现了Placeholder。

private UILabel _placeholderLabel;

通过使用反射,我可以获得一个指向UILabel _placeholderLabel的指针,但我无法移动它。
如果有人能帮我移动UILabel,我相信我们就有了答案!

protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
{
    base.OnElementChanged(e);

    if (Control != null)
    {
        editor = e.NewElement as EditorEx;

        //Use reflection to access private placeholder in parent
        //https://www.c-sharpcorner.com/UploadFile/6f0898/how-to-access-a-private-member-of-a-class-from-other-class/
        System.Reflection.FieldInfo receivedObject = typeof(EditorRenderer).GetFields(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)[1];

        _placeholderLabel = receivedObject.GetValue(this) as UILabel;

        _placeholderLabel.Text = "move me!!; //this works
        _placeholderLabel.BackgroundColor = UIColor.Green; //this works

        //put it on another message to see if that solves it (it doesn't)
        Device.BeginInvokeOnMainThread(() =>
        {
            //HELP NEEDED HERE!!!
            //_placeholderLabel.Frame = new CGRect(-15, -10, 500, 30); //doesn't work
            //_placeholderLabel.Bounds = new CGRect(-15, -10, 500, 30); //doesn't work
            _placeholderLabel.LayoutMargins = new UIEdgeInsets(-15, -10, 5, 5); //doesn't work

        });
    }
}

相关问题