直接上代码
.h
#import <UIKit/UIKit.h>
@interface LabelInSet : UILabel
@property (nonatomic, assign) UIEdgeInsets insets;
@end
.m
#import "LabelInSet.h"
@implementation LabelInSet
/**
* 重写initWithFrame
* @param frame frame
* @return LabelInSet
*/
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.insets = UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, 0.0f);
}
return self;
}
/**
* 重写drawTextInRect,根据insets绘制Label
* @param rect CGRect
*/
- (void)drawTextInRect:(CGRect)rect {
UIEdgeInsets insets = _insets;
[super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}
@end