iOS-UILabel中文字與邊框間距的自定義

項(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一樣,到此大功告成!

最后編輯于
?著作權(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)容