iOSUILable邊距設(shè)置

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)單呢 哈哈 ?。。。?/p>

使用實(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)注明出處,謝謝?。?!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容