Xamarin表单 - 自定义FontPamily,DatePicker的FontSize

时间:2016-08-22 09:03:04

标签: datepicker xamarin.forms customization

我搜索了如何更改DatePicker Entry的字体大小/字体系列,但是我找不到任何想法?

谢谢!

3 个答案:

答案 0 :(得分:15)

您可以通过自定义渲染器执行此操作:

例如,我会将渲染器用于 iOS Control将是每个平台的原生元素。你可以用原生方式改变它。

[assembly: ExportRenderer(typeof(DatePicker), typeof(CustomDatePickerRenderer))]
namespace SuperForms.Samples.iOS
{
    public class CustomDatePickerRenderer : DatePickerRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<DatePicker> e)
        {
            base.OnElementChanged(e);

            //Control is UITextField
            var someFontWithName = UIFont.FromName("fontName", 14);
            UIFont font = Control.Font.WithSize(14);
            Control.Font = font;
        }
    }
}

<强>更新

对于Android:

[assembly: ExportRenderer(typeof(DatePicker), typeof(CustomDatePickerRenderer))]
namespace SuperForms.Samples.Droid
{
    public class CustomDatePickerRenderer : DatePickerRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<DatePicker> e)
        {
            base.OnElementChanged(e);

            // Control is Android.Widget.EditText
            Control.Typeface = Android.Graphics.Typeface.CreateFromAsset(Forms.Context.Assets, "Path/To/YourFont.otf");

            Control.TextSize = 14;
        }
    }    
}

答案 1 :(得分:2)

**Xamarin Forms** => This is for **Android**, using **custom renderer** we can change the font size of datepicker.
[assembly: ExportRenderer(typeof(DatePicker), typeof(CustomDatePickerRenderer))]
namespace  Samples.Droid
{
    public class CustomDatePickerRenderer : DatePickerRenderer
    {
         protected override void        OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.DatePicker> e)
        {
            base.OnElementChanged(e);
            **Control.TextSize = 12;**

         }
    }
}

Thank you

答案 2 :(得分:2)

我不知道你现在是否需要回答,我仍然附上解决日期选择器字体大小问题的代码而不使用自定义渲染..

创建<DatePicker x:Name="PickerDate" MinimumDate="05-24-2017" Format="mm-dd-yyyy" MaximumDate="12-31-2050" IsVisible="False" DateSelected="PickerDate_DateSelected"/> <Label x:Name="DateLabel" Text="Tap to Select Date" FontSize="14" HorizontalOptions="Start"> <Label.GestureRecognizers> <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped_1"/> </Label.GestureRecognizers> </Label> 文件

MainPage.Xaml.cs

创建private void TapGestureRecognizer_Tapped_1(object sender, EventArgs e) { PickerDate.Focus(); } private void PickerDate_DateSelected(object sender, DateChangedEventArgs e) { DateLabel.Text = PickerDate.Date.Date.ToString("MM-dd-yyyy"); } 文件

DECLARE @command NVARCHAR(1000)--, @filename NVARCHAR(100)

SELECT @command = '
BEGIN
DECLARE @filename nvarchar(256)
USE ?
SELECT @filename = 
    ''F:\DevworxExtStorage\Databases\MSSQL12.DEVSQL2014\MSSQL\DATA\'' + DB_NAME() + ''.md''
END 

SELECT @filename
    '
exec sp_MSforeachdb @command

这解决了Android和IOS中的问题。