UIButton垂直对齐不起作用

时间:2022-07-14 21:08:56

I can't figure why in the following code, the title alignment isn't remain Top.

我无法理解为什么在下面的代码中,标题对齐不是顶部。

UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn2.titleLabel.font = [UIFont systemFontOfSize:53];
btn2.frame = CGRectMake(20, 20, 270, 44);
[btn2 setTitle:@"test1 test2 test3 test4 test5 test6 test7" forState:UIControlStateNormal];
[btn2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
btn2.titleLabel.minimumFontSize = 1.0;
btn2.titleLabel.adjustsFontSizeToFitWidth = YES;
btn2.titleLabel.numberOfLines = 1;
btn2.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;

UIButton垂直对齐不起作用

3 个解决方案

#1


2  

This behavior is due to the baselineAdjustment default property of the button's titleLabel. If you set this to UIBaselineAdjustmentNone, you should get the effect you're looking for.

此行为是由按钮的titleLabel的baselineAdjustment默认属性引起的。如果将其设置为UIBaselineAdjustmentNone,则应该获得您正在寻找的效果。

btn2.titleLabel.baselineAdjustment = UIBaselineAdjustmentNone;

btn2.titleLabel.baselineAdjustment = UIBaselineAdjustmentNone;

From the docs for UILabel:

来自UILabel的文档:

baselineAdjustment

Controls how text baselines are adjusted when text needs to shrink to fit in the label.

控制在文本需要缩小以适合标签时如何调整文本基线。

@property(nonatomic) UIBaselineAdjustment baselineAdjustment

Discussion

If the adjustsFontSizeToFitWidth property is set to YES, this property controls the behavior of the text baselines in situations where adjustment of the font size is required. The default value of this property is UIBaselineAdjustmentAlignBaselines. This property is effective only when the numberOfLines property is set to 1.

如果adjustsFontSizeToFitWidth属性设置为YES,则此属性控制在需要调整字体大小的情况下文本基线的行为。此属性的默认值为UIBaselineAdjustmentAlignBaselines。仅当numberOfLines属性设置为1时,此属性才有效。

and

UIBaselineAdjustmentAlignBaselines

Adjust text relative to the position of its baseline.

相对于其基线位置调整文本。

Available in iOS 2.0 and later.

适用于iOS 2.0及更高版本。

UIBaselineAdjustmentAlignCenters

Adjust text based relative to the center of its bounding box.

相对于其边界框的中心调整文本。

Available in iOS 2.0 and later.

适用于iOS 2.0及更高版本。

UIBaselineAdjustmentNone

Adjust text relative to the top-left corner of the bounding box. This is the default adjustment.

相对于边界框的左上角调整文本。这是默认调整。

Available in iOS 2.0 and later.

适用于iOS 2.0及更高版本。

Note that the default adjustment for UILabel differs from that of a button's titleLabel.

请注意,UILabel的默认调整与按钮的titleLabel的默认调整不同。

#2


5  

UIButton has a very nifty property named "titleEdgeInsets" which you can use (via UIEdgeInsetsMake to reposition the top and bottom margins of the title and get the thing centered, vertically.

UIButton有一个非常漂亮的属性名为“titleEdgeInsets”,您可以使用它(通过UIEdgeInsetsMake重新定位标题的顶部和底部边距,并使事物垂直居中。

#3


0  

UIButton垂直对齐不起作用

Have a look at Content-Alignment Vertical in storyboard

在故事板中查看Content-Alignment Vertical

#1


2  

This behavior is due to the baselineAdjustment default property of the button's titleLabel. If you set this to UIBaselineAdjustmentNone, you should get the effect you're looking for.

此行为是由按钮的titleLabel的baselineAdjustment默认属性引起的。如果将其设置为UIBaselineAdjustmentNone,则应该获得您正在寻找的效果。

btn2.titleLabel.baselineAdjustment = UIBaselineAdjustmentNone;

btn2.titleLabel.baselineAdjustment = UIBaselineAdjustmentNone;

From the docs for UILabel:

来自UILabel的文档:

baselineAdjustment

Controls how text baselines are adjusted when text needs to shrink to fit in the label.

控制在文本需要缩小以适合标签时如何调整文本基线。

@property(nonatomic) UIBaselineAdjustment baselineAdjustment

Discussion

If the adjustsFontSizeToFitWidth property is set to YES, this property controls the behavior of the text baselines in situations where adjustment of the font size is required. The default value of this property is UIBaselineAdjustmentAlignBaselines. This property is effective only when the numberOfLines property is set to 1.

如果adjustsFontSizeToFitWidth属性设置为YES,则此属性控制在需要调整字体大小的情况下文本基线的行为。此属性的默认值为UIBaselineAdjustmentAlignBaselines。仅当numberOfLines属性设置为1时,此属性才有效。

and

UIBaselineAdjustmentAlignBaselines

Adjust text relative to the position of its baseline.

相对于其基线位置调整文本。

Available in iOS 2.0 and later.

适用于iOS 2.0及更高版本。

UIBaselineAdjustmentAlignCenters

Adjust text based relative to the center of its bounding box.

相对于其边界框的中心调整文本。

Available in iOS 2.0 and later.

适用于iOS 2.0及更高版本。

UIBaselineAdjustmentNone

Adjust text relative to the top-left corner of the bounding box. This is the default adjustment.

相对于边界框的左上角调整文本。这是默认调整。

Available in iOS 2.0 and later.

适用于iOS 2.0及更高版本。

Note that the default adjustment for UILabel differs from that of a button's titleLabel.

请注意,UILabel的默认调整与按钮的titleLabel的默认调整不同。

#2


5  

UIButton has a very nifty property named "titleEdgeInsets" which you can use (via UIEdgeInsetsMake to reposition the top and bottom margins of the title and get the thing centered, vertically.

UIButton有一个非常漂亮的属性名为“titleEdgeInsets”,您可以使用它(通过UIEdgeInsetsMake重新定位标题的顶部和底部边距,并使事物垂直居中。

#3


0  

UIButton垂直对齐不起作用

Have a look at Content-Alignment Vertical in storyboard

在故事板中查看Content-Alignment Vertical