码迷,mamicode.com
首页 > 其他好文 > 详细

UILabel+Create

时间:2014-11-06 19:53:33      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   ar   使用   sp   div   on   

#import <UIKit/UIKit.h>

@interface UILabel (Create)

/**
 *  创建普通Label
 *
 *  @param frame          frame
 *  @param text           text
 *  @param font           font
 *  @param textColor      textColor
 *  @param backgroudColor backgroudColor
 *  @param textAlignment  textAlignment
 *
 *  @return UILabel
 */
+ (UILabel *)createLabelWithFrame:(CGRect)frame text:(NSString *)text font:(UIFont *)font textColor:(UIColor *)textColor backgroudColor:(UIColor *)backgroudColor  textAlignment:(NSTextAlignment)textAlignment;

/**
 *  创建自增高Label
 *
 *  @param frame          frame
 *  @param text           text
 *  @param font           font
 *  @param textColor      textColor
 *  @param backgroudColor backgroudColor
 *  @param textAlignment  textAlignment
 *
 *  @return UILabel
 */
+ (UILabel *)createAdjustsLabelWithFrame:(CGRect)frame text:(NSString *)text font:(UIFont *)font textColor:(UIColor *)textColor backgroudColor:(UIColor *)backgroudColor  textAlignment:(NSTextAlignment)textAlignment;

@end

#import "UILabel+Create.h"

@implementation UILabel (Create)

+ (UILabel *)createLabelWithFrame:(CGRect)frame text:(NSString *)text font:(UIFont *)font textColor:(UIColor *)textColor backgroudColor:(UIColor *)backgroudColor  textAlignment:(NSTextAlignment)textAlignment
{
    UILabel *label = [[UILabel alloc]initWithFrame:frame];
    label.text = text;
    [label setFont:font];
    [label setTextColor:textColor];
    if (backgroudColor == nil) {
        [label setBackgroundColor:[UIColor clearColor]];
    }
    else{
        [label setBackgroundColor:backgroudColor];
    }
    [label setTextAlignment:textAlignment];
    label.numberOfLines = 0;
    
    return  label;
}

+ (UILabel *)createAdjustsLabelWithFrame:(CGRect)frame text:(NSString *)text font:(UIFont *)font textColor:(UIColor *)textColor backgroudColor:(UIColor *)backgroudColor  textAlignment:(NSTextAlignment)textAlignment
{
    UILabel *label = [[UILabel alloc]initWithFrame:frame];
    label.text = text;
    [label setFont:font];
    [label setTextColor:textColor];
    if (backgroudColor == nil) {
        [label setBackgroundColor:[UIColor clearColor]];
    }
    else{
        [label setBackgroundColor:backgroudColor];
    }
    [label setTextAlignment:textAlignment];
    label.numberOfLines = 0;
    
    CGSize maxNameLabelSize = CGSizeMake(frame.size.width,2000);
    CGSize labelSize;
    labelSize = [text boundingRectWithSize:maxNameLabelSize
                                   options:NSStringDrawingUsesLineFragmentOrigin
                                attributes:@{NSFontAttributeName:font}
                                   context:nil].size;
    [label setFrame:CGRectMake(frame.origin.x, frame.origin.y, labelSize.width, labelSize.height)];
    
    return  label;
}

// 使用
[self.view addSubview:[UILabel createLabelWithFrame:CGRectMake(10, 100, self.view.frame.size.width-20, 30) text:_str font:[UIFont systemFontOfSize:14.f] textColor:[UIColor redColor] backgroudColor:[UIColor clearColor] textAlignment:NSTextAlignmentLeft]];

 

UILabel+Create

标签:style   blog   io   color   ar   使用   sp   div   on   

原文地址:http://www.cnblogs.com/joesen/p/4079380.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!