Xamrin.Forms Entry Cell How to change font size for Placeholder

為{幸葍}努か 提交于 2019-12-12 09:10:54

问题


I am thinking if i can change font size for placeholder on EntryCell without change fontSize for Text on EntryCell?

Is There away to change font size for Placeholder without change font size for Text

 Code = new EntryCell { Label = "Code:*", Text = "", Keyboard = Keyboard.Default, Placeholder = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" };

回答1:


Here is a custom renderer for android. Here I'm modifying the HintTextColor(placeholder). You can modify the font in a similar way.

  using System;
using Xamarin.Forms.Platform.Android;
using Xamarin.Forms;
using communityhealth;
using Android.Graphics;
using communityhealth.Android;


[assembly: ExportRenderer (typeof (MyUsernameEntry), typeof (MyUsernameEntryRenderer))]
[assembly: ExportRenderer (typeof (MyPasswordEntry), typeof (MyPasswordEntryRenderer))]
[assembly: ExportRenderer (typeof (MyEntry), typeof (MyEntryRenderer))]

namespace communityhealth.Android
{
    public class MyEntryRenderer : EntryRenderer
    {
        protected override void OnElementChanged (ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged (e);
            if (e.OldElement == null) {   // perform initial setup
                // lets get a reference to the native control
                var nativeEditText = (global::Android.Widget.EditText) Control;
                // do whatever you want to the textField here!
                nativeEditText.SetBackgroundColor(global::Android.Graphics.Color.Transparent);
                nativeEditText.SetTextColor(global::Android.Graphics.Color.White);
                Typeface font = Typeface.CreateFromAsset (Forms.Context.Assets, "Neris-Light.otf");
                nativeEditText.TextSize = 14f;
                nativeEditText.Typeface = font;
            }
        }
    }

    public class MyUsernameEntryRenderer : MyEntryRenderer
    {
        protected override void OnElementChanged (ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged (e);

            if (e.OldElement == null) {
                // lets get a reference to the native control
                var nativeEditText = (global::Android.Widget.EditText) Control;
                nativeEditText.Hint = "Username";
                nativeEditText.SetHintTextColor (global::Android.Graphics.Color.White);
                nativeEditText.TextSize = 18f;
            }
        }
    }

    public class MyPasswordEntryRenderer : MyEntryRenderer
    {
        protected override void OnElementChanged (ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged (e);

            if (e.OldElement == null) {
                // lets get a reference to the native control
                var nativeEditText = (global::Android.Widget.EditText) Control;
                nativeEditText.Hint = "Password";
                nativeEditText.SetHintTextColor (global::Android.Graphics.Color.White);
                nativeEditText.TextSize = 18f;
            }
        }
    }
}



回答2:


Unfortunately, no.

There is no API in Forms to change the Placeholder font size. Instead, you could create your own custom control to do this, or use a custom renderer to modify the placholder in the native view.



来源:https://stackoverflow.com/questions/36284250/xamrin-forms-entry-cell-how-to-change-font-size-for-placeholder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!