在iOS中Lable是沒有UIEdgeInsets這個(gè)屬性可以調(diào)用的本慕,那么我們想修改下Lable的上下左右的邊距該怎么辦呢?例如:
那么現(xiàn)在來實(shí)現(xiàn)下侧漓,代碼如下:
1.首先創(chuàng)建一個(gè)繼承UILable的類
2.增加UIEdgeInsets屬性
#import?
@interfacecustomBaseLab?:?UILabel
/**
*?lable文字的邊距
*/
@property(nonatomic,?assign)?UIEdgeInsets?textLableInsets;
@end
3..m實(shí)現(xiàn)如下:
-?(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)];
}
是不是很簡(jiǎn)單呢 哈哈 9尽!2颊帷藤违!
使用實(shí)例:
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);//調(diào)用
[self.viewaddSubview:yearLab];
技術(shù)有限 就到這,請(qǐng)大神多多指點(diǎn)纵揍;轉(zhuǎn)載請(qǐng)注明出處顿乒,謝謝!T蠼鳌璧榄!