底部的iOS中心文字

时间:2012-05-11 19:45:13

标签: ios text label vertical-alignment

我有一个使用以下方式创建的标签:

UILabel *label = [[UILabel alloc] initWithFrame:cell.contentView.bounds];
label.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
label.textAlignment = UITextAlignmentCenter;

我想将标签的底部居中并水平居中。 我没有在typedef枚举中找到任何UITextAlignmentBottom。

有人可以提出解决方案吗?

2 个答案:

答案 0 :(得分:0)

对齐'在底部的文本中,您需要使UILabel成为需要显示的文本的高度 - Google UILabel sizeToFit

答案 1 :(得分:0)

我并不完全确定你的意思是“将标签置于标签的底部”,但我想你可能会问如何将标签置于视图控制器底部的中心位置。为此,您需要添加一些NSLayoutConstraint来指定标签在窗口中的位置。

要以编程方式执行此操作,您将使用constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:。 “属性”参数指的是NSLayoutAttribute,在尝试使标签居中时可以设置为NSLayoutAttributeCenterXNSLayoutAttributeBottom指定标签与底部之间的空间量。查看控制器。