項目中需要這個效果,于是找度娘,問谷歌,按照其中一位作者的思路自己動手封裝;
自定義一個繼承于UILabel的Label,直接上代碼;
想到邊距,首先熟悉的一個詞就是UIEdgeInsets
@property(nonatomic, assign) UIEdgeInsets edgeInsets;
外界可以通過這個屬性來更改我們這個自定義的Label的邊距;
最重要的就是在.m文件中我們要重寫兩個方法
- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines;
- (void)drawTextInRect:(CGRect)rect;
cmd點擊進(jìn)去之后,你會看見官方給的兩句注釋,大概理解下
// override points. can adjust rect before calling super.
// label has default content mode of UIViewContentModeRedraw
閱讀iOS官方文檔后,其中第一個方法中bounds
這個參數(shù)給出解釋是
The bounding rectangle of the receiver.
第二個參數(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)用這個方法。當(dāng)你重寫了這個方法之后,使用時應(yīng)該調(diào)用sizeToFit
這個方法,不然的話,這個方法不會被調(diào)用。英語不好,湊合著看。
第二個方法
- (void)drawTextInRect:(CGRect)rect;
官方文檔也說了
The rectangle in which to draw the text.
需要一個可以描繪出label的矩形來作為這個方法的參數(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
大概就是說這個也是你不能直接調(diào)用的方法,需要在子類中重寫。調(diào)用此方法的時候,當(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)切一個rect出來;
然后在需要創(chuàng)建內(nèi)切Label的時候創(chuàng)建,給之前自定義的屬性賦值
showLabel.edgeInsets = UIEdgeInsetsMake(8, 8+2, 8, 8+2);//設(shè)置內(nèi)邊距
根據(jù)官方文檔的說法,我們還得調(diào)用一下
[showLabel sizeToFit];//重新計算尺寸,會執(zhí)行Label內(nèi)重寫的方法
其他設(shè)置跟普通label一樣,到此大功告成!