iOS控件裏除了UIButton能夠調節字體到左右邊界的距離,其餘控件都不能夠,但能夠自定義實現。字體
.h能夠這樣寫:atom
// #import "InsetsLabel.h".net
@interface InsetsLabel : UILabelget
@property(nonatomic) UIEdgeInsets insets;it
-(id) initWithFrame:(CGRect)frame andInsets: (UIEdgeInsets) insets;io
-(id) initWithInsets: (UIEdgeInsets) insets;class
@endimport
.m能夠這樣寫方法
@implementation InsetsLabelim
@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
關鍵是重寫drawTextInRect方法。
這樣能夠經過insets屬性來調節字符距離上下左右的距離。