您的位置:首页 > 产品设计 > UI/UE

UILabel居上对齐居下对齐类别扩展

2014-04-19 15:10 351 查看
实现原理:当该label不足以达到要求的高度时,居上对齐时在页末补“\n空格”,居下对齐时在页首补“空格\n”

UILabel+VerticalAlign.h

@interface UILabel(VerticalAlign)
-(void)alignTop;
-(void)alignBottom;
@end


UILabel+VerticalAlign.m

@implementation UILabel(VerticalAlign)
-(void)alignTop {
CGSize fontSize =[self.text sizeWithFont:self.font];
double finalHeight = self.frame.size.height;//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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: