iOS 开发中超级简单的自适应宽度与自适应高度

2017-12-29  本文已影响0人  冯龙胜

废话不多说直接上代码

.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

上一篇下一篇

猜你喜欢

热点阅读