0

I created a custom renderer for a toast. In that, I need to provide padding at the left.

  UIViewController alert = new UIViewController();

  UILabel view = new UILabel();
  int DeviceWidth = (int)UIScreen.MainScreen.Bounds.Width;

  float position = (DeviceWidth - DialogWidth) / 2;
  view.Frame = new CoreGraphics.CGRect(position, 0, DialogWidth, 40);
            
  view.Text = message;
            
  view.Text.ToString().PadLeft(20, ' ');

 alert.View.Add(view);
 UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(alert, true, null);
Priyanka
  • 138
  • 2
  • 15
  • Does this answer your question? [UILabel with padding in Xamarin.iOS?](https://stackoverflow.com/questions/21164506/uilabel-with-padding-in-xamarin-ios) – Jason Aug 17 '21 at 11:10

1 Answers1

0

The quickest way : add a extra UIView , add the UILabel onside it and adjust the X coordinate to simulate the padding effect.

refer to the sample code

void ShowAlert(string message, double seconds)
        {
            alertDelay = NSTimer.CreateScheduledTimer(seconds, (obj) =>
            {
                dismissMessage();
            });
            alert = new UIViewController();

            int DeviceWidth = (int)UIScreen.MainScreen.Bounds.Width;
            float position = (DeviceWidth - DialogWith) / 2;


            //add a exta view
            UIView superview = new UIView();
            superview.Frame = new CoreGraphics.CGRect(position, 0, DialogWith, 30);

            superview.BackgroundColor = UIColor.Yellow;


            UILabel view = new UILabel();
            var frame = superview.Bounds;
            frame.X = 20;
            frame.Width -= 20;
            view.Frame = frame;

            view.Text = message;
            view.BackgroundColor = UIColor.Clear;
            view.TextColor = UIColor.Red;


            superview.Add(view);
            alert.View.Add(superview);
            UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(alert, true, null);
        }

enter image description here

ColeX
  • 14,062
  • 5
  • 43
  • 240