美文网首页
UILable顶部对齐,底部对齐

UILable顶部对齐,底部对齐

作者: 面试小集 | 来源:发表于2015-10-13 16:36 被阅读282次
 @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```

相关文章

网友评论

      本文标题:UILable顶部对齐,底部对齐

      本文链接:https://www.haomeiwen.com/subject/tfuocttx.html