Inherits UILabel and reloads the drawTextInRect method.
// 1. header file
# Import <UIKit/UIKit. h>
@ 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
Call part
InsetsLabel * lblTitle = [[InsetsLabel alloc] initWithFrame: CGRectMake (0, 35 + 25 * I, 185, 22)];
[LblTitle setInsets: UIEdgeInsetsMake (0, 5, 0, 5)];