将文本垂直对齐到UILabel中的顶部我有一个UILabel有两行文字的空格。有时,当文本太短时,此文本将显示在标签的垂直中心。如何垂直对齐文本,使其始终位于UILabel?
3 回答
慕标琳琳
TA贡献1830条经验 获得超9个赞
设置新的文本: myLabel.text = @"Some Text"
设置 maximum number
为0行(自动): myLabel.numberOfLines = 0
将标签的框架设置为最大大小: myLabel.frame = CGRectMake(20,20,200,800)
打电话 sizeToFit
以缩小帧大小,使内容只适合: [myLabel sizeToFit]
慕森卡
TA贡献1806条经验 获得超8个赞
for(int i=1; i< newLinesToPad; i++) self.text = [self.text stringByAppendingString:@"\n"];
for(int i=0; i<newLinesToPad; i++) self.text = [self.text stringByAppendingString:@"\n "];
UILabels
@" \n@%"
"\n@%"
// -- file: UILabel+VerticalAlign.h#pragma mark VerticalAlign@interface UILabel (VerticalAlign)- (void)alignTop; - (void)alignBottom;@end// -- file: UILabel+VerticalAlign.m@implementation UILabel (VerticalAlign)- (void)alignTop { CGSize fontSize = [self.text sizeWithFont:self.font]; double finalHeight = fontSize.height * self.numberOfLines; double finalWidth = self.frame.size.width; //expected width of label CGSize theStringSize = [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(finalWidth, finalHeight) lineBreakMode:self.lineBreakMode]; int newLinesToPad = (finalHeight - theStringSize.height) / fontSize.height; for(int i=0; i<newLinesToPad; i++) self.text = [self.text stringByAppendingString:@"\n "];}- (void)alignBottom { CGSize fontSize = [self.text sizeWithFont:self.font]; double finalHeight = fontSize.height * self.numberOfLines; double finalWidth = self.frame.size.width; //expected width of label CGSize theStringSize = [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(finalWidth, finalHeight) lineBreakMode:self.lineBreakMode]; int newLinesToPad = (finalHeight - theStringSize.height) / fontSize.height; for(int i=0; i<newLinesToPad; i++) self.text = [NSString stringWithFormat:@" \n%@",self.text];}@end
[yourLabel alignTop];
[yourLabel alignBottom];
- 3 回答
- 0 关注
- 764 浏览
添加回答
举报
0/150
提交
取消