Ios 以编程方式将UILabel文本对齐到底部?
有没有办法通过编程将UILabel文本与框架底部对齐 它应该是什么样子的:Ios 以编程方式将UILabel文本对齐到底部?,ios,objective-c,alignment,uilabel,vertical-alignment,Ios,Objective C,Alignment,Uilabel,Vertical Alignment,有没有办法通过编程将UILabel文本与框架底部对齐 它应该是什么样子的: 1 line of text: ____________ | | | | | | | | | some text1 | ____________ 2 lines of text: ____________ | | | | | | | some text1
1 line of text:
____________
| |
| |
| |
| |
| some text1 |
____________
2 lines of text:
____________
| |
| |
| |
| some text1 |
| some text2 |
____________
您可以按照答案中的说明扩展UILabel类: 编辑:
由于
sizeWithFont
已弃用,您可以尝试此解决方案。我还没有测试过,但你可以通过我=一次。
问题的可能重复是,
sizeWithFont
已被弃用。我真的不知道应该用什么来代替它。文档对此非常清楚:使用。
#pragma mark VerticalAlign
@interface UILabel (VerticalAlign)
- (void)alignTop;
- (void)alignBottom;
@end
@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
[myLabel alignBottom];