在iOS中Lable是没有UIEdgeInsets这个属性可以调用的,那么我们想修改下Lable的上下左右的边距该怎么办呢?例如:
那么现在来实现下,代码如下:
1.首先创建一个继承UILable的类
2.增加UIEdgeInsets属性
#import
@interfacecustomBaseLab : UILabel
/**
* lable文字的边距
*/
@property(nonatomic, assign) UIEdgeInsets textLableInsets;
@end
3..m实现如下:
- (instancetype)init {
if(self= [superinit]) {
_textInsets = UIEdgeInsetsZero;
}
returnself;
}
- (instancetype)initWithFrame:(CGRect)frame {
if(self= [superinitWithFrame:frame]) {
_textInsets = UIEdgeInsetsZero;
}
returnself;
}
- (void)drawTextInRect:(CGRect)rect {
[superdrawTextInRect:UIEdgeInsetsInsetRect(rect, _textInsets)];
}
是不是很简单呢 哈哈 !!!!
使用实例:
customBaseLab*yearLab = [[customBaseLaballoc]initWithFrame:CGRectMake(0,0,self.viewWidth-30,self.viewHeight)];
yearLab.backgroundColor= [UIColorwhiteColor];
yearLab.text=@"2012";
yearLab.textColor= [UIColorgrayColor];
yearLab.font= [UIFontsystemFontOfSize:16.0f];
yearLab.textInsets= UIEdgeInsetsMake(0,15,0,0);//调用
[self.viewaddSubview:yearLab];
技术有限 就到这,请大神多多指点;转载请注明出处,谢谢!!!