繁体   English   中英

旋转 UILabel 的拉伸宽度

[英]Stretched width for rotated UILabel

抱歉,我真的不知道原生 iOS 设计如何工作(我通常做 Xamarin Forms 设计)。 我有UIStackView

UIStackView stackView = new UIStackView(new CGRect(0, 0, View.Frame.Width, View.Frame.Height));

然后我创建UILabel ,设置AdjustsFontSizeToFitWidth = true因为我需要自动调整字体大小

之后我对其应用一些旋转targetLabel.Transform = CGAffineTransform.MakeRotation(new nfloat(Math.PI * 270 / 180.0)); 因为我需要制作垂直文本。

然后我将此 label 添加到堆栈中。 当然我也有一些其他的设置,但我认为它们无关紧要。

我的最后一步是为UILabel设置具有固定宽度的新Frame 我尝试通过获取堆栈的高度来设置宽度,我什至尝试将其设置为常量,但它不起作用。 label 的宽度总是看起来像汽车。

这就是我要的:

在此处输入图像描述

但实际上,目标label很小。

完整代码在这里:(注意这是 TodayExtension)

[Register("CodeBasedViewController")]
    public class CodeBasedViewController : SLComposeServiceViewController, INCWidgetProviding
    {
        UIImage image;
        private UILabel otherLabel;
        private UILabel targetLabel;

        public CodeBasedViewController(IntPtr handle) : base(handle)
        {
        }

        public override void ViewWillAppear(bool animated)
        {
            base.ViewWillAppear(animated);

                UIStackView stackView = new UIStackView(new CGRect(0, 0, View.Frame.Width, View.Frame.Height));
                View.AddSubview(stackView);
                stackView.Axis = UILayoutConstraintAxis.Horizontal;
                image = new UIImage("picture.png");
                UIImageView imageView = new UIImageView(image)
                {
                    ContentMode = UIViewContentMode.ScaleAspectFit
                };
                otherLabel = new UILabel
                {
                    TextColor = UIColor.White,
                    Text = "someTextA",
                    Font = UIFont.SystemFontOfSize(40)
                };
                targetLabel = new UILabel
                {
                    TextColor = UIColor.Black,
                    Text ="4",
                    TextAlignment = UITextAlignment.Left,
                    Font = UIFont.SystemFontOfSize(30),
        AdjustsFontSizeToFitWidth = true,
                    Lines = 1
                };
                targetLabel.Transform = CGAffineTransform.MakeRotation(new nfloat(Math.PI * 270 / 180.0));
                stackView.AddArrangedSubview(imageView);
                stackView.AddArrangedSubview(otherLabel);
                stackView.AddArrangedSubview(targetLabel);
                stackView.Frame = new CGRect(View.Frame.Width / 2, 0,
                    otherLabel .IntrinsicContentSize.Width +
                    targetLabel.IntrinsicContentSize.Width +
                    View.Frame.Height
                    , View.Frame.Height);
                stackView.Center = View.Center;

        targetLabel.Frame = new CGRect(targetLabel.Frame.X, targetLabel.Frame.Y, 150, 150); //test constants

        }

        public void WidgetPerformUpdate(Action<NCUpdateResult> completionHandler)
        {
            completionHandler(NCUpdateResult.NewData);
        }

    }

首先,设置 StackView 的属性Distribution

stackView.Distribution = UIStackViewDistribution.Fill;

同时不要忘记设置label的字体

targetLabel.Frame = new CGRect(targetLabel.Frame.X, targetLabel.Frame.Y, 150, 150);
targetLabel.Font = UIFont.SystemFontOfSize(80);

以下是完整代码,我设置框架只是为了测试,你可以设置你想要的框架(如果你想设置等于屏幕的大小,你应该同时设置label和imageView的框架)。

UIStackView stackView = new UIStackView(new CGRect(100,200,300,150));
View.AddSubview(stackView);
stackView.Distribution = UIStackViewDistribution.Fill;
stackView.Spacing = 10;
stackView.BackgroundColor = UIColor.LightGray;
stackView.Axis = UILayoutConstraintAxis.Horizontal;

UIImageView imageView = new UIImageView()
{
  ContentMode = UIViewContentMode.ScaleAspectFit,
  BackgroundColor = UIColor.Red           
};

otherLabel = new UILabel
{
  TextColor = UIColor.Black,
  Text = "someTextA",
  Font = UIFont.SystemFontOfSize(40)
};

targetLabel = new UILabel
{
  TextColor = UIColor.Black,
  Text = "4",
  TextAlignment = UITextAlignment.Left,
  Font = UIFont.SystemFontOfSize(100),
  AdjustsFontSizeToFitWidth = true,
  Lines = 0
};

targetLabel.Transform = CGAffineTransform.MakeRotation(new nfloat(Math.PI * 270 / 180.0));
stackView.AddArrangedSubview(imageView);
stackView.AddArrangedSubview(otherLabel);
stackView.AddArrangedSubview(targetLabel);

stackView.Center = View.Center;

targetLabel.Frame = new CGRect(targetLabel.Frame.X, targetLabel.Frame.Y, 150, 150);

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM