項(xiàng)目中需要這個(gè)效果提陶,于是找度娘烫沙,問谷歌隙笆,按照其中一位作者的思路自己動(dòng)手封裝锌蓄;
自定義一個(gè)繼承于UILabel的Label,直接上代碼瘸爽;
想到邊距,首先熟悉的一個(gè)詞就是UIEdgeInsets
@property(nonatomic, assign) UIEdgeInsets edgeInsets;
外界可以通過這個(gè)屬性來更改我們這個(gè)自定義的Label的邊距;
最重要的就是在.m文件中我們要重寫兩個(gè)方法
- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines;
- (void)drawTextInRect:(CGRect)rect;
cmd點(diǎn)擊進(jìn)去之后荧琼,你會(huì)看見官方給的兩句注釋譬胎,大概理解下
// override points. can adjust rect before calling super.
// label has default content mode of UIViewContentModeRedraw
閱讀iOS官方文檔后,其中第一個(gè)方法中bounds
這個(gè)參數(shù)給出解釋是
The bounding rectangle of the receiver.
第二個(gè)參數(shù)numberOfLines
給出的解釋是
The maximum number of lines to use for the label.
The value 0 indicates there is no maximum number of lines and that the rectangle should encompass all of the text.
返回值CGRect類型的解釋是
The computed drawing rectangle for the label’s text.
怎么用呢命锄?接著看文檔
This method should only be overridden by subclasses that
want to change the receiver’s bounding rectangle before performing any other computations.
Use the value in the numberOfLines
parameter to limit the height of the returned rectangle to the specified number of lines of text.
This method may be called by the system if there was a prior call to the sizeToFit
or sizeThatFits: method. Note that labels in UITableViewCell
objects are sized based on the cell dimensions, and not a requested size.
大概意思就是說這是需要在子類中重寫的方法堰乔,你不能直接去調(diào)用這個(gè)方法。當(dāng)你重寫了這個(gè)方法之后累舷,使用時(shí)應(yīng)該調(diào)用sizeToFit
這個(gè)方法,不然的話夹孔,這個(gè)方法不會(huì)被調(diào)用被盈。英語不好,湊合著看搭伤。
第二個(gè)方法
- (void)drawTextInRect:(CGRect)rect;
官方文檔也說了
The rectangle in which to draw the text.
需要一個(gè)可以描繪出label的矩形來作為這個(gè)方法的參數(shù)只怎;
使用方法也說了
You should not call this method directly.
This method should only be overridden by subclasses that
want to modify the default drawing behavior for the label’s text.
By the time this method is called, the current graphics context is already configured with
the default environment and text color for drawing.
In your overridden method, you can configure the current context further and then invoke super
to do the actual drawing or you can do the drawing yourself.
If you do render the text yourself, you should not invoke super
大概就是說這個(gè)也是你不能直接調(diào)用的方法,需要在子類中重寫怜俐。調(diào)用此方法的時(shí)候,當(dāng)前圖形上下文已經(jīng)配置了默認(rèn)的繪圖環(huán)境和文本顏色身堡。如果不是自己渲染的話,調(diào)用[super drawTextInRect:rect];
看了之后我們就重寫方法即可
- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines {
UIEdgeInsets insets = self.edgeInsets;
CGRect rect = [super textRectForBounds:UIEdgeInsetsInsetRect(bounds, insets)
limitedToNumberOfLines:numberOfLines];
rect.origin.x -= insets.left;
rect.origin.y -= insets.top;
rect.size.width += (insets.left + insets.right);
rect.size.height += (insets.top + insets.bottom);
return rect;
}
- (void)drawTextInRect:(CGRect)rect {
[super drawTextInRect:UIEdgeInsetsInsetRect(rect, self.edgeInsets)];
}
其中UIEdgeInsetsInsetRect
表示在原來的rect基礎(chǔ)上根據(jù)邊緣距離內(nèi)切一個(gè)rect出來拍鲤;
然后在需要?jiǎng)?chuàng)建內(nèi)切Label的時(shí)候創(chuàng)建,給之前自定義的屬性賦值
showLabel.edgeInsets = UIEdgeInsetsMake(8, 8+2, 8, 8+2);//設(shè)置內(nèi)邊距
根據(jù)官方文檔的說法贴谎,我們還得調(diào)用一下
[showLabel sizeToFit];//重新計(jì)算尺寸汞扎,會(huì)執(zhí)行Label內(nèi)重寫的方法
其他設(shè)置跟普通label一樣,到此大功告成擅这!