UIlabel根据文字的长度来动态设置的大小
#import <Foundation/Foundation.h>
@interface SINALabelUtils : NSObject
+(UILabel *) updateLabel:(UILabel*) tagLabel andWithString:(NSString*)tagString;
+(UILabel *) updateLabel:(UILabel*) tagLabel andWithString:(NSString*)tagString andWithTextSize:(NSInteger) fontSize;
+(UILabel *)updateLabel:(UILabel *)tagLabel andWithString:(NSString *)tagString andWithTagWidth:(NSInteger) tagWidth andWithTagHeight:(NSInteger) tagHeight andWithTagFontSize:(NSInteger)fontSize;
@end
#import "SINALabelUtils.h"
@implementation SINALabelUtils
+(UILabel *)updateLabel:(UILabel *)tagLabel andWithString:(NSString *)tagString{
return [[self class] updateLabel:tagLabel andWithString:tagString andWithTagWidth:[UIScreen mainScreen].bounds.size.width andWithTagHeight:[UIScreen mainScreen].bounds.size.height andWithTagFontSize:16];
}
+(UILabel *)updateLabel:(UILabel *)tagLabel andWithString:(NSString *)tagString andWithTextSize:(NSInteger)fontSize{
return [[self class] updateLabel:tagLabel andWithString:tagString andWithTagWidth:[UIScreen mainScreen].bounds.size.width andWithTagHeight:[UIScreen mainScreen].bounds.size.height andWithTagFontSize:fontSize];
}
+(UILabel *)updateLabel:(UILabel *)tagLabel andWithString:(NSString *)tagString andWithTagWidth:(NSInteger) tagWidth andWithTagHeight:(NSInteger) tagHeight andWithTagFontSize:(NSInteger)fontSize{
NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize],};
CGSize textSize = [tagString boundingRectWithSize:CGSizeMake(tagWidth, tagHeight) options:NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;;
tagLabel.numberOfLines=0;
tagLabel.frame=CGRectMake(tagLabel.frame.origin.x, tagLabel.frame.origin.y, textSize.width, textSize.height);
return tagLabel;
}
@end