UILable顶部对齐,底部对齐

2015-10-13  本文已影响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```
上一篇 下一篇

猜你喜欢

热点阅读