0

I know this question has been asked a lot but I've tried the various solutions and can't seem to find one that works. I have a label on my storyboard titled Messages. On button click, different text appears in the label. I need to pad just this label.

I've looked at: Adding space/padding to a UILabel, UILabel text margin, and Resizing a UILabel to accommodate insets.

Also not sure where in my ViewController.cs to put the code. I put it under public partial class ViewController : UIViewController but get errors.

Edit 2:

Okay, at first I did a UITextView but couldn't get vertical align to work so went back to label. This is what I have:

public partial class ViewController : UIViewController
{

    public partial class PaddedUILabel : UILabel
    {
        private UIEdgeInsets EdgeInsets { get; set; }

        public PaddedUILabel()
        {
            EdgeInsets = new UIEdgeInsets(0, 10, 0, 10);
        }
        public override void DrawText(CoreGraphics.CGRect rect)
        {
            base.DrawText(EdgeInsets.InsetRect(rect));
        }
    }

public override void ViewDidLoad()
    {

        base.ViewDidLoad();

        Message.Layer.BorderWidth = 1;
        Message.BackgroundColor = UIColor.FromWhiteAlpha(1, 0.88f);
        PaddedUILabel _paddedUILabel = Message as PaddedUILabel;

I'm still not getting any padding.

Zailyn Tamayo
  • 112
  • 1
  • 9

2 Answers2

0

You can either use a UITextView and provide the insets directly (as mentioned in this link) which you can do either in ViewDidLoad or ViewWillAppear, or you can subclass your UILabel and override the DrawText method.

The DrawText method helps manipulate the rectangle in which the text within a label is drawn. I'd suggest you pay around with some values to get started with it.

Something like:

public partial class PaddedUILabel : UILabel
{
     //Override draw text here

}

If you are using Xamarin iOS Native, give your label(for example: myLabel) the custom class from story board and retrieve the label as:

PaddedUILabel _paddedUILabel = this.myLabel as PaddedUILabel;

I'm sorry I would have given you the whole code but as of now, don't have access to a Mac environment. Let me know if you need anything else.

For vertical align in TextView follow link.

Cheers

Singhal2
  • 450
  • 7
  • 22
0

i tried many solutions as discussed here but the actual out coming not what i wanted so i made it like that :

 if (this.Padding != default(Thickness))
                {
                    Device.BeginInvokeOnMainThread(() =>
                      {

                          if (Parent is Grid)
                          {
                              var parentAsGrid = Parent as Grid;
                              var index = parentAsGrid.Children.IndexOf(this);
                              parentAsGrid.Children.Remove(this);
                              Grid marginGrid = new Grid() { BackgroundColor = this.BackgroundColor,  HorizontalOptions = this.HorizontalOptions, VerticalOptions = this.VerticalOptions };


                              var lbl = new Label() { Text = this.Text, TextColor = this.TextColor, BackgroundColor = this.BackgroundColor, HorizontalOptions = this.HorizontalOptions, VerticalOptions = this.VerticalOptions, FontSize = this.FontSize };
                              lbl.Margin = this.Padding;
                              if (!parentAsGrid.Children.Contains(this))
                              {
                                  marginGrid.Children.Add(lbl);
                                  parentAsGrid.Children.Insert(index, marginGrid);
                              }

                          }
                          if (Parent is StackLayout)
                          {
                              var parentAsGrid = Parent as StackLayout;
                              var index = parentAsGrid.Children.IndexOf(this);
                              parentAsGrid.Children.Remove(this);
                              Grid marginGrid = new Grid() { BackgroundColor = this.BackgroundColor,   HorizontalOptions = this.HorizontalOptions, VerticalOptions = this.VerticalOptions };


                              var lbl = new Label() { Text = this.Text, TextColor = this.TextColor, BackgroundColor = this.BackgroundColor, HorizontalOptions = this.HorizontalOptions, VerticalOptions = this.VerticalOptions, FontSize = this.FontSize };
                              lbl.Margin = this.Padding;
                              if (!parentAsGrid.Children.Contains(this))
                              {
                                  marginGrid.Children.Add(lbl);
                                  parentAsGrid.Children.Insert(index, marginGrid);
                              }

                          }



                      });
Abdullah Tahan
  • 1,963
  • 17
  • 28