【问题标题】:Xamrin.Forms Entry Cell How to change font size for PlaceholderXamarin.Forms 输入单元格如何更改占位符的字体大小
【发布时间】:2016-07-17 00:08:03
【问题描述】:

我正在考虑是否可以更改 EntryCell 上占位符的字体大小而不更改 fontSizeEntryCell 上的文本?

是否可以在不更改文本字体大小的情况下更改占位符的字体大小

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

【问题讨论】:

    标签: c# xamarin xamarin.forms portable-class-library


    【解决方案1】:

    这是一个用于 android 的自定义渲染器。在这里,我正在修改 HintTextColor(占位符)。您可以以类似的方式修改字体。

      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;
                }
            }
        }
    }
    

    【讨论】:

    • 谢谢@darrellbooker .. 当我有时间尝试时,我会接受你的回答:)
    【解决方案2】:

    很遗憾,没有。

    Forms 中没有用于更改占位符字体大小的 API。相反,您可以创建自己的自定义控件来执行此操作,或者使用自定义渲染器来修改本机视图中的占位符。

    【讨论】:

    • 能否请您添加一些我正在开发android项目的代码。
    猜你喜欢
    • 2019-05-23
    • 1970-01-01
    • 2020-02-23
    • 2020-02-12
    • 2019-04-01
    • 1970-01-01
    • 2014-03-15
    • 2019-02-17
    • 2011-07-16
    相关资源
    最近更新 更多