创建UILabel

UILabelCreate.h
#import <UIKit/UIKit.h>

@interface UILabelCreate : UILabel

/**
 *  创建UILabel 初始化不包含text
 *
 *  @param frame          frame
 *  @param font           font
 *  @param textColor      textColor
 *  @param backgroudColor backgroudColor
 *  @param textAlignment  textAlignment
 */
- (void)createNormalLabel:(CGRect)frame font:(UIFont *)font textColor:(UIColor *)textColor backgroudColor:(UIColor *)backgroudColor  textAlignment:(NSTextAlignment)textAlignment;

/**
 *  创建UILabel 初始化包含text
 *
 *  @param frame          frame
 *  @param text           text
 *  @param font           font
 *  @param textColor      textColor
 *  @param backgroudColor backgroudColor
 *  @param textAlignment  textAlignment
 */
- (void)createDefaultLabel:(CGRect)frame text:(NSString *)text font:(UIFont *)font textColor:(UIColor *)textColor backgroudColor:(UIColor *)backgroudColor  textAlignment:(NSTextAlignment)textAlignment;

/**
 *  创建UILabel 初始化包含text(自增高)
 *
 *  @param frame          frame
 *  @param text           text
 *  @param font           font
 *  @param textColor      textColor
 *  @param backgroudColor backgroudColor
 *  @param textAlignment  textAlignment
 */
- (void)createLabelAdjustsFontSizeOfHigh:(CGRect)frame text:(NSString *)text font:(UIFont *)font textColor:(UIColor *)textColor backgroudColor:(UIColor *)backgroudColor  textAlignment:(NSTextAlignment)textAlignment;

≈
 *  设置Text并返回Label的高
 *
 *  @param text           text
 *  @param lableSizeWidth Label's Width
 *
 *  @return Label's higt
 */
- (CGFloat)setTextWithLabelSizeHigh:(NSString *)text lableSizeWidth:(CGFloat)lableSizeWidth;

---------------------------------------------------------------------------------------------------- UILabelCreate.m
#define IOS7_BEFORE ([[[UIDevice currentDevice] systemVersion] floatValue] <7.0 ? YES : NO) #import "UILabelCreate.h" @implementation UILabelCreate - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code } return self; } /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ - (void)createNormalLabel:(CGRect)frame font:(UIFont *)font textColor:(UIColor *)textColor backgroudColor:(UIColor *)backgroudColor textAlignment:(NSTextAlignment)textAlignment { [self setFrame:frame]; [self setFont:font]; [self setTextColor:textColor]; if (backgroudColor == nil) { [self setBackgroundColor:[UIColor clearColor]]; } else{ [self setBackgroundColor:backgroudColor]; } [self setTextAlignment:textAlignment]; self.numberOfLines = 0; } - (void)createDefaultLabel:(CGRect)frame text:(NSString *)text font:(UIFont *)font textColor:(UIColor *)textColor backgroudColor:(UIColor *)backgroudColor textAlignment:(NSTextAlignment)textAlignment { [self setFrame:frame]; [self setText:text]; [self setFont:font]; [self setTextColor:textColor]; if (backgroudColor == nil) { [self setBackgroundColor:[UIColor clearColor]]; } else{ [self setBackgroundColor:backgroudColor]; } [self setTextAlignment:textAlignment]; self.numberOfLines = 0; } - (void)createLabelAdjustsFontSizeOfHigh:(CGRect)frame text:(NSString *)text font:(UIFont *)font textColor:(UIColor *)textColor backgroudColor:(UIColor *)backgroudColor textAlignment:(NSTextAlignment)textAlignment { [self setText:text]; [self setFont:font]; [self setTextColor:textColor]; if (backgroudColor == nil) { [self setBackgroundColor:[UIColor clearColor]]; } else{ [self setBackgroundColor:backgroudColor]; } [self setTextAlignment:textAlignment]; self.numberOfLines = 0; CGSize maxNameLabelSize = CGSizeMake(frame.size.width,2000); CGSize labelSize; if (IOS7_BEFORE) { labelSize = [text sizeWithFont:font constrainedToSize:maxNameLabelSize lineBreakMode:NSLineBreakByWordWrapping]; } else{ labelSize = [text boundingRectWithSize:maxNameLabelSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil].size; } [self setFrame:CGRectMake(frame.origin.x, frame.origin.y, labelSize.width, labelSize.height)]; } - (CGFloat)setTextWithLabelSizeHigh:(NSString *)text lableSizeWidth:(CGFloat)lableSizeWidth { [self setText:text]; CGSize maxNameLabelSize = CGSizeMake(lableSizeWidth,2000); CGSize labelSize; if (IOS7_BEFORE) { labelSize = [text sizeWithFont:self.font constrainedToSize:maxNameLabelSize lineBreakMode:NSLineBreakByWordWrapping]; } else{ NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: self.font,NSFontAttributeName, nil]; labelSize = [text boundingRectWithSize:maxNameLabelSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size; } return labelSize.height; }
原文地址:https://www.cnblogs.com/joesen/p/3779887.html