項目中須要這個效果,因而找度娘,問谷歌,按照其中一位做者的思路本身動手封裝;bash
自定義一個繼承於UILabel的Label,直接上代碼;ide
想到邊距,首先熟悉的一個詞就是UIEdgeInsets
ui
@property(nonatomic, assign) UIEdgeInsets edgeInsets;
複製代碼
外界能夠經過這個屬性來更改咱們這個自定義的Label的邊距;this
最重要的就是在.m文件中咱們要重寫兩個方法atom
- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines;
- (void)drawTextInRect:(CGRect)rect;
複製代碼
cmd點擊進去以後,你會看見官方給的兩句註釋,大概理解下spa
// override points. can adjust rect before calling super.
// label has default content mode of UIViewContentModeRedraw
複製代碼
閱讀iOS官方文檔後,其中第一個方法中bounds
這個參數給出解釋是code
The bounding rectangle of the receiver.
複製代碼
第二個參數numberOfLines
給出的解釋是orm
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.
複製代碼
怎麼用呢?接着看文檔ci
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.
複製代碼
大概意思就是說這是須要在子類中重寫的方法,你不能直接去調用這個方法。當你重寫了這個方法以後,使用時應該調用sizeToFit
這個方法,否則的話,這個方法不會被調用。英語很差,湊合着看。
第二個方法 - (void)drawTextInRect:(CGRect)rect;
官方文檔也說了
The rectangle in which to draw the text.
複製代碼
須要一個能夠描繪出label的矩形來做爲這個方法的參數; 使用方法也說了
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
複製代碼
大概就是說這個也是你不能直接調用的方法,須要在子類中重寫。調用此方法的時候,當前圖形上下文已經配置了默認的繪圖環境和文本顏色。若是不是本身渲染的話,調用[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基礎上根據邊緣距離內切一個rect出來;
而後在須要建立內切Label的時候建立,給以前自定義的屬性賦值
showLabel.edgeInsets = UIEdgeInsetsMake(8, 8+2, 8, 8+2);//設置內邊距
複製代碼
根據官方文檔的說法,咱們還得調用一下
[showLabel sizeToFit];//從新計算尺寸,會執行Label內重寫的方法
複製代碼
其餘設置跟普通label同樣,到此大功告成!