首页 > 其他 > 详细

UILABEL 增加居上对齐 居下对齐方法

时间:2014-04-22 23:17:55      阅读:573      评论:0      收藏:0      [点我收藏+]

.h文件

#import <UIKit/UIKit.h>

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

.m文件

bubuko.com,布布扣
#import "UILabel+VerticalAlign.h"

@implementation UILabel(VerticalAlign)
-(void)alignTop {
    CGSize fontSize = [@"一行" sizeWithFont:self.font constrainedToSize:CGSizeMake(self.frame.size.width, INT64_MAX) lineBreakMode:NSLineBreakByWordWrapping];//单行高度[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, 2000) 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
bubuko.com,布布扣

 

UILABEL 增加居上对齐 居下对齐方法,布布扣,bubuko.com

UILABEL 增加居上对齐 居下对齐方法

原文:http://www.cnblogs.com/hushuai-ios/p/3679783.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!