设置UILabel的内间距

来源:互联网 发布:c 定义json数组 编辑:程序博客网 时间:2024/04/30 06:20

因为UILabel没有相关属性设置内间距,只能自己重新自定义

//1.header file

    #import

    @interface InsetsLabel : UILabel

    @property(nonatomic) UIEdgeInsets insets;

    -(id) initWithFrame.:(CGRect)frame. andInsets: (UIEdgeInsets) insets;

    -(id) initWithInsets: (UIEdgeInsets) insets;

    @end

    //2. implementation file

    @implementation InsetsLabel

    @synthesize insets=_insets;

    -(id) initWithFrame.:(CGRect)frame. andInsets:(UIEdgeInsets)insets {

    self = [super initWithFrame.:frame];

    if(self){

    self.insets = insets;

    }

    return self;

    }

    -(id) initWithInsets:(UIEdgeInsets)insets {

    self = [super init];

    if(self){

    self.insets = insets;

    }

    return self;

    }

    -(void) drawTextInRect:(CGRect)rect {

    return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, self.insets)];

    }

    @end

    调用部分

    InsetsLabel * lblTitle=[[InsetsLabel alloc] initWithFrame.:CGRectMake(0, 35+25*i, 185, 22)];

    [lblTitle setInsets:UIEdgeInsetsMake(0, 5, 0, 5)];

0 0
原创粉丝点击