UILabel
如何从底部对齐。比方说,我的标签可以容纳三行文本。如果输入文本是单行,那么该行应该位于标签的底部。请引用下图以便更好地理解。橙色区域是标签的全框。目前它只有一行,并且居中对齐。所以我想要的是,无论有多少行,它都应该始终与底部对齐。
请提出您的想法。
谢谢。
最佳答案
这里有两种方法...
1. 首先将 numberOfLines
设置为 0,然后使用 UILabel
的 sizeToFit
属性,这样您的 UILabel
以其 contentSize
显示。
yourLabel.numberOfLines = 0;
[yourLabel sizeToFit];
从此链接查看更多信息:Vertically align text within a UILabel
2. 另一种选择是采用 UITextField
而不是 UILabel
并将 userInteractionEnabled
设置为 NO
像下面...
[yourTextField setUserInteractionEnabled:NO];
然后将 contentVerticalAlignment
属性设置为底部,如下所示...
[yourTextField setContentVerticalAlignment:UIControlContentVerticalAlignmentBottom];
更新
此外,使用 UITextField
,我们无法实现多行。因此,我们可以使用 UITextView
并将其 userInteractionEnabled
设置为 NO
。然后,使用下面的代码使其底部对齐。
CGFloat topCorrect = ([label bounds].size.height - [label contentSize].height);
topCorrect = (topCorrect <0.0 ? 0.0 : topCorrect);
label.contentOffset = (CGPoint){.x = 0, .y = -topCorrect};
关于iphone - 如何从底部对齐 UILabel 文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18247934/