【问题标题】:iOS Center Text on Bottom底部的 iOS 中心文本
【发布时间】:2012-05-20 10:06:12
【问题描述】:
我有一个使用以下方式创建的标签:
UILabel *label = [[UILabel alloc] initWithFrame:cell.contentView.bounds];
label.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
label.textAlignment = UITextAlignmentCenter;
我想将标签底部的 居中并水平居中。
我在 typedef 枚举中没有找到任何 UITextAlignmentBottom。
有人可以提出解决方案吗?
【问题讨论】:
标签:
ios
text
label
vertical-alignment
【解决方案1】:
要将文本“对齐”到底部,您需要将 UILabel 设置为它需要显示的文本的高度 - Google UILabel sizeToFit。
【解决方案2】:
我不完全确定“在标签底部居中”是什么意思,但我想你可能会问如何在它所在的视图控制器底部居中标签。为此,您需要添加一些NSLayoutConstraints 来指定标签在窗口中的位置。
要以编程方式执行此操作,您将使用constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:。 “attribute”参数指的是NSLayoutAttribute,当试图使标签居中时可以设置为NSLayoutAttributeCenterX,NSLayoutAttributeBottom指定标签和视图控制器底部之间的空间量。