Xamrin.Forms 条目单元格如何更改占位符的字体大小
Xamrin.Forms Entry Cell How to change font size for Placeholder
我在想是否可以更改 EntryCell
上占位符的字体大小而不更改 EntryCell
上文本的 fontSize
?
是否可以更改占位符的字体大小而不更改文本的字体大小
Code = new EntryCell { Label = "Code:*", Text = "", Keyboard = Keyboard.Default, Placeholder = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" };
很遗憾,没有。
表单中没有 API 可以更改占位符字体大小。相反,您可以创建自己的自定义控件来执行此操作,或使用自定义渲染器修改本机视图中的占位符。
这是 android 的自定义渲染器。我在这里修改 HintTextColor(placeholder)。您可以用类似的方式修改字体。
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;
}
}
}
}
我在想是否可以更改 EntryCell
上占位符的字体大小而不更改 EntryCell
上文本的 fontSize
?
是否可以更改占位符的字体大小而不更改文本的字体大小
Code = new EntryCell { Label = "Code:*", Text = "", Keyboard = Keyboard.Default, Placeholder = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" };
很遗憾,没有。
表单中没有 API 可以更改占位符字体大小。相反,您可以创建自己的自定义控件来执行此操作,或使用自定义渲染器修改本机视图中的占位符。
这是 android 的自定义渲染器。我在这里修改 HintTextColor(placeholder)。您可以用类似的方式修改字体。
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;
}
}
}
}