【发布时间】:2019-09-20 22:29:11
【问题描述】:
对不起,我真的不知道原生 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));,因为我需要制作垂直文本。
然后我将此标签添加到堆栈中。当然我也有一些其他的设置,但我认为它们无关紧要。
我的最后一步是以固定宽度为UILabel 设置新的Frame。我尝试通过获取堆栈的高度来设置宽度,我什至尝试将其设置为常量,但它不起作用。标签的宽度总是看起来像自动。
这就是我想要的:
但其实目标标签很小。
完整代码在这里:(注意这是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);
}
}
【问题讨论】:
标签: c# ios xamarin xamarin.ios