关于iOS UILabel 设置 UIEdgeInsets

直接上代码
.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

猜你喜欢

转载自blog.csdn.net/whde_006/article/details/51509521