Forum Xamarin Xamarin.Forms

Label android custom renderer (SetIncludeFontPadding doesn't work)

DenniroDenniro Member ✭✭
edited December 2019 in Xamarin.Forms

Hi,
I can't set the padding of a label to zero. The Top and Bottom padding always take place.
Here is my code:

Custom Renderer for android

  using System;
  using System.Collections.Generic; 
  using System.Linq;
  using System.Text;
  using Android.App;
  using Android.Content;
  using Android.OS;
  using Android.Runtime;
  using Android.Views;
  using Android.Widget;
  using TestApp.CustomRenderers;
  using TestApp.Droid.CustomRenderers;
  using Xamarin.Forms;
  using Xamarin.Forms.Platform.Android;

 [assembly:ExportRenderer(typeof(LabelCustomBorderless),typeof(LabelCustomBorderlessAndroid))]
namespace TestApp.Droid.CustomRenderers
{
   public class LabelCustomBorderlessAndroid : LabelRenderer
  {
    public LabelCustomBorderlessAndroid(Context context) : base(context)
    {
    }

    protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
    {
        base.OnElementChanged(e);
        Control?.SetIncludeFontPadding(false);
    }
  }
 }`

Custom class that inherits from Label class

 using System;
 using System.Collections.Generic;
 using System.Text;
 using Xamarin.Forms;

namespace TestApp.CustomRenderers
{
   public class LabelCustomBorderless : Label
  {
  }
}

`

Xaml file

`<?xml version="1.0" encoding="utf-8" ?>

<ContentPage.Content>
    <StackLayout BackgroundColor="#f2f4f5" Spacing="1">

        <CustomRenderers:LabelCustomBorderless Text="90" FontSize="40" TextColor="White"
                            BackgroundColor="Blue"/>
</StackLayout>
</ContentPage.Content>`

Am i missing something ?
Thanks in advance!
Regards

Answers

Sign In or Register to comment.