iOS 如何沿倾斜轴对齐 UIViews / UILabels?
iOS How to align UIViews / UILabels along a angled axis?
我想设置一系列 X 中心沿 45 度角对齐的 UILabel。
我想我可以用数学方法做到这一点,但有没有一种方法可以画一条 45 度线,然后将每个 UILabel 分配到沿着该 45 度线的不同点?
您可以为标签的 center-x, center-y 设置一个约束,使其成为包含视图的宽度/高度的百分比。这是通用视图控制器上下文中的一个简单示例:
for ( CGFloat m = 0.25 ; m < 1 ; m += .25 ) {
UILabel* label = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, 100, 40)];
label.text = @"Hello, World";
label.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview: label];
NSLayoutConstraint* lcx = [NSLayoutConstraint constraintWithItem: label
attribute: NSLayoutAttributeCenterX
relatedBy: NSLayoutRelationEqual
toItem: self.view
attribute: NSLayoutAttributeRight
multiplier: m
constant: 0];
NSLayoutConstraint* lcy = [NSLayoutConstraint constraintWithItem: label
attribute: NSLayoutAttributeCenterY
relatedBy: NSLayoutRelationEqual
toItem: self.view
attribute: NSLayoutAttributeBottom
multiplier: m
constant: 0];
[self.view addConstraint: lcx];
[self.view addConstraint: lcy];
}
我想设置一系列 X 中心沿 45 度角对齐的 UILabel。
我想我可以用数学方法做到这一点,但有没有一种方法可以画一条 45 度线,然后将每个 UILabel 分配到沿着该 45 度线的不同点?
您可以为标签的 center-x, center-y 设置一个约束,使其成为包含视图的宽度/高度的百分比。这是通用视图控制器上下文中的一个简单示例:
for ( CGFloat m = 0.25 ; m < 1 ; m += .25 ) {
UILabel* label = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, 100, 40)];
label.text = @"Hello, World";
label.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview: label];
NSLayoutConstraint* lcx = [NSLayoutConstraint constraintWithItem: label
attribute: NSLayoutAttributeCenterX
relatedBy: NSLayoutRelationEqual
toItem: self.view
attribute: NSLayoutAttributeRight
multiplier: m
constant: 0];
NSLayoutConstraint* lcy = [NSLayoutConstraint constraintWithItem: label
attribute: NSLayoutAttributeCenterY
relatedBy: NSLayoutRelationEqual
toItem: self.view
attribute: NSLayoutAttributeBottom
multiplier: m
constant: 0];
[self.view addConstraint: lcx];
[self.view addConstraint: lcy];
}