c# - Xamarin.Forms 输入单元格如何更改占位符的字体大小

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

我在想是否可以更改 EntryCell 上占位符的字体大小而不更改 EntryCell 上文本的 fontSize

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

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

最佳答案

这是一个适用于 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;
            }
        }
    }
}

关于c# - Xamarin.Forms 输入单元格如何更改占位符的字体大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36284250/

相关文章:

c# - 设置 Xamarin.Forms - "No resource found that matches the given name..."

c# - 从 Unity 中的 Tilemap 获取邻居

xamarin - 使用Xamarin.Firebase.Messaging对FirebaseInstanceId.Instance.Token进行了描述,并在Xamarin.Android中返回null

c# - 在异步方法完成之前,UI 不会更新

c# - .NET 6.0 中的 Null 行为

c# - 如何在 .NET 中安装 GestureRecognizer 组件?

c# - 使用 Xamarin.iOS 获取 iOS 库文件夹的正确方法是什么?

c# - 在 xamarin 中启用 ARC

xaml - ListView 不触发 ItemTapped

android - Xamarin.Auth 收到 disallowed_useragent 错误 [Xamarin.Forms]