控制UIlabel 垂直方向对齐方式的 方法
2014-05-21 22:31
423 查看
最正统的方法,利用objective-c的category特性,修改UILabel的绘制代码。示例代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | // -- 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 |
相关文章推荐
- js控制图片缩放、水平和垂直方向居中对齐
- js控制图片缩放、水平和垂直方向居中对齐
- UITextField垂直水平对齐方式代码控制
- js控制图片缩放、水平和垂直方向居中对齐
- 弹性盒布局-----指定水平方向与垂直方向上的对齐方式
- JS实现控制表格行内容垂直对齐的方法
- 让 UILabel 垂直方向顶端对齐的代码
- UILabel 垂直方向顶端对齐
- IOS开发之----让UILabel 垂直方向顶端对齐的代码
- 盒布局下的水平方向和垂直方向的对齐方式
- 让 UILabel 垂直方向顶端对齐的代码
- iOS开发之----让UILabel 垂直方向顶端对齐的代码
- JS实现控制表格单元格垂直对齐的方法
- UILabel文本垂直顶部对齐的方法
- 让UILabel垂直方向顶端对齐
- js控制图片缩放、水平和垂直方向居中对齐
- 让 UILabel 垂直方向顶端对齐的代码
- 分享一个可垂直顶端对齐的UILabel
- 基于vertical-align的表单元素垂直对齐方式研究 By 小强 @ 2009年10月27日 [ 前端表现 ]
- 中文字,文本框,button按钮垂直居中对齐方法