【问题标题】:How to provide a padding to the UILable programaticalli in xamarin.iOS如何在 xamarin.iOS 中以编程方式向 UILabel 提供填充
【发布时间】:2021-08-17 10:56:15
【问题描述】:

我为 toast 创建了一个自定义渲染器。在那,我需要在左边提供填充。

  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);

【问题讨论】:

标签: xamarin.ios customization padding


【解决方案1】:

最快的方法:多加一个UIView,在上面加上UILabel,然后调整X坐标来模拟填充效果。

参考示例代码

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);
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-05
    • 2021-08-16
    • 2015-09-22
    • 1970-01-01
    • 2011-09-25
    • 1970-01-01
    • 1970-01-01
    • 2018-01-22
    相关资源
    最近更新 更多