1. 程式人生 > >UILabel-文字內邊距

UILabel-文字內邊距

自定義UILabel類myLabel

myLabel.h檔案中宣告一個新的init方法:

- (instancetype)initWithFrame:(CGRect)frame insets:(UIEdgeInsets)insets;
myLabel.m檔案中實現drawTextInRect方法:
@implementation myLabel
{
    UIEdgeInsets _insets;
}

- (instancetype)initWithFrame:(CGRect)frame insets:(UIEdgeInsets)insets
{
    if (self = [superinitWithFrame:frame]) {
        _insets = insets;
    }
    returnself;
}


- (void)drawTextInRect:(CGRect)rect
{
    return [superdrawTextInRect:UIEdgeInsetsInsetRect(rect,_insets)];
}