废话不多说直接上代码
.h 文件
@interfaceUILabel (QYPayHeightAndWidth)
+ (CGFloat)getHeightByWidth:(CGFloat)width title:(NSString*)title font:(UIFont*)font;
+ (CGFloat)getWidthWithTitle:(NSString*)title font:(UIFont*)font;
@end
.m 文件
#import"UILabel+QYPayHeightAndWidth.h"
@implementationUILabel (QYPayHeightAndWidth)
+ (CGFloat)getHeightByWidth:(CGFloat)width title:(NSString*)title font:(UIFont*)font
{
UILabel*label = [[UILabelalloc]initWithFrame:CGRectMake(0,0, width,0)];
label.text= title;
label.font= font;
label.numberOfLines=0;
[labelsizeToFit];
CGFloatheight = label.frame.size.height;
return height;
}
+ (CGFloat)getWidthWithTitle:(NSString*)title font:(UIFont*)font {
UILabel*label = [[UILabelalloc]initWithFrame:CGRectMake(0,0,1000,0)];
label.text= title;
label.font= font;
[labelsizeToFit];
return label.frame.size.width;
}
@end
网友评论