Sorry i'm really can't figure out how is native iOS design works (i'm usually do Xamarin Forms design). I have UIStackView
UIStackView stackView = new UIStackView(new CGRect(0, 0, View.Frame.Width, View.Frame.Height));
then i create UILabel
, set AdjustsFontSizeToFitWidth = true
because i need auto adjusted font size
after that i apply some rotation to it targetLabel.Transform = CGAffineTransform.MakeRotation(new nfloat(Math.PI * 270 / 180.0));
because i need to make vertical text.
and then i add this label to the stack. Of course i have some other setups too but i think they are doesn't matter.
my final step is to set new Frame
for UILabel
with fixed width. I tried set width by getting stack's height, i tried even set it to constant, but it doesn't work. Width of the label always looks like auto.
this is what i want:
but in fact, the target label is very small.
Full code here: (Note this is 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);
}
}
Firstly, set the property Distribution
of StackView
stackView.Distribution = UIStackViewDistribution.Fill;
And don't forget to set the Font of label at the same time
targetLabel.Frame = new CGRect(targetLabel.Frame.X, targetLabel.Frame.Y, 150, 150);
targetLabel.Font = UIFont.SystemFontOfSize(80);
Following is the full code, I set the frame just for test and you can set the frame as you want(if you want to set the equals the size of screen, you should set the frame of label and imageView at same time).
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.