UILable的封装

/**

 创建普通文本


 @param frame        frame

 @param text          文字

 @param textColor    字体颜色

 @param textAlignment 字体位置

 @param font          字体大小


 @return label

 */

+ (instancetype)createLabelWithFrame:(CGRect)frame text:(NSString *)text titleColor:(UIColor *)textColor textAlignment:(NSTextAlignment)textAlignment font:(CGFloat)font;

//带行间距的lable

+ (instancetype)createLabelWithFrame:(CGRect)frame text:(NSString *)text titleColor:(UIColor *)textColor textAlignment:(NSTextAlignment)textAlignment font:(CGFloat)font lineSpacing:(CGFloat)lineSpacing;

/**

 获取普通文本的高度  (影响普通文本高度的两个因素:1.最大宽度 2.字体大小)

 @param maxWidth    最大宽度限制

 @return label的高度

 */

- (CGFloat)getLabelHeightWithMaxWidth:(CGFloat)maxWidth;

/**

 获取富文本高度  (影响富文本高度的4个因素:1.最大宽度 2.字体大小 3.行间距  4.字间距  字间距一般不变,这里没有封装)

 @param text      text

 @param maxWidth  最大宽度限制

 @param lineSpacing 行间距

 @return label的高度

 */

- (CGFloat)getAttributeLabelHeightWithText:(NSString*)text maxWidth:(CGFloat)maxWidth lineSpacing:(CGFloat)lineSpacing;


.m文件中

@implementationUILabel (Category)

+ (instancetype)createLabelWithFrame:(CGRect)frame text:(NSString *)text titleColor:(UIColor *)textColor textAlignment:(NSTextAlignment)textAlignment font:(CGFloat)font

{

    UILabel *label = [[UILabel alloc]initWithFrame:frame];

    label.text= text;

    label.textColor= textColor;

    label.textAlignment= textAlignment;

    label.font= [UIFont systemFontOfSize:font];

    returnlabel;

}

+ (instancetype)createLabelWithFrame:(CGRect)frame text:(NSString *)text titleColor:(UIColor *)textColor textAlignment:(NSTextAlignment)textAlignment font:(CGFloat)font lineSpacing:(CGFloat)lineSpacing{

    UILabel *label = [[UILabel alloc]initWithFrame:frame];

    label.text= text;

    label.textColor= textColor;

    label.textAlignment= textAlignment;

    label.font= [UIFont systemFontOfSize:font];

    NSMutableAttributedString *attText = [[NSMutableAttributedString alloc]initWithString:text];

    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc]init];

    style.lineSpacing= lineSpacing;

    [attText setAttributes:@{NSParagraphStyleAttributeName:style}range:NSMakeRange(0, text.length)];

    label.attributedText= attText;

    returnlabel;

}

- (CGFloat)getLabelHeightWithMaxWidth:(CGFloat)maxWidth

{

    self.numberOfLines = 0;

    self.width= maxWidth;

    CGRect rect = [self.text boundingRectWithSize:CGSizeMake(maxWidth, MAXFLOAT)  options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:self.font,} context:nil];


    returnrect.size.height;

}

-(CGFloat)getAttributeLabelHeightWithText:(NSString *)text maxWidth:(CGFloat)maxWidth lineSpacing:(CGFloat)lineSpacing

{

    self.numberOfLines = 0;

    self.width= maxWidth;

    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];

    [style setLineSpacing:lineSpacing];

    CGRect rect = [text boundingRectWithSize:CGSizeMake(maxWidth, MAXFLOAT)  options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:self.font,NSParagraphStyleAttributeName:style} context:nil];

    returnrect.size.height;

}

你可能感兴趣的:(UILable的封装)