iOSUILable边距设置

   在iOS中Lable是没有 UIEdgeInsets这个属性可以调用的,那么我们想修改下Lable的上下左右的边距该怎么办呢?例如:iOSUILable边距设置_第1张图片 

那么现在来实现下,代码如下:

1.首先创建一个继承UILable的类

2.增加 UIEdgeInsets属性

#import 

@interface customBaseLab : UILabel
/**
 * lable文字的边距
 */
@property (nonatomic, assign) UIEdgeInsets textLableInsets;

@end
3..m实现如下:

- (instancetype)init {
    if (self = [super init]) {
        _textInsets = UIEdgeInsetsZero;
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        _textInsets = UIEdgeInsetsZero;
    }
    return self;
}

- (void)drawTextInRect:(CGRect)rect {
    [super drawTextInRect:UIEdgeInsetsInsetRect(rect, _textInsets)];
}
是不是很简单呢 哈哈 !!!!

使用实例:

   customBaseLab *yearLab = [[customBaseLab alloc] initWithFrame:CGRectMake(0, 0, self.viewWidth-30, self.viewHeight)];
    yearLab.backgroundColor = [UIColor whiteColor];
    yearLab.text = @"2012";
    yearLab.textColor = [UIColor grayColor];
    yearLab.font = [UIFont systemFontOfSize:16.0f];
    yearLab.textInsets = UIEdgeInsetsMake(0, 15, 0, 0);//调用
    [self.view addSubview: yearLab];
技术有限 就到这,请大神多多指点;转载请注明出处,谢谢!!!

你可能感兴趣的:(Obj-C)