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属性来调节字符距离上下左右的距离。