iOS-設置UILabel的內邊距


 
https://www.jianshu.com/p/d8d29e30d2d4
2016.07.01 17:25* 字數 211 閱讀 24373評論 18
 
 
Paste_Image.png

問題說明

默認Label的顯示效果如下


很多情況下,需要如下有內邊距的效果(注意第一行與最后一行文字與label的上下邊距

@interface SFLabel : UILabel

// 用來決定上下左右內邊距,也可以提供一個借口供外部修改,在這里就先固定寫死

@property (assign, nonatomic) UIEdgeInsets edgeInsets;

@end

 

 

#import "SFLabel.h"

 

@implementation SFLabel

 

 

//下面三個方法用來初始化edgeInsets

- (instancetype)initWithFrame:(CGRect)frame

{

    if(self = [super initWithFrame:frame])

    {

        self.edgeInsets = UIEdgeInsetsMake(25, 0, 25, 0);

    }

    return self;

}

 

- (instancetype)initWithCoder:(NSCoder *)aDecoder

{

    if (self = [super initWithCoder:aDecoder]) {

        self.edgeInsets = UIEdgeInsetsMake(25, 0, 25, 0);

    }

    return self;

}

 

- (void)awakeFromNib

{

    [super awakeFromNib];

    self.edgeInsets = UIEdgeInsetsMake(25, 0, 25, 0);

}

 

// 修改繪制文字的區域,edgeInsets增加bounds

-(CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines

{

    

    /*

     調用父類該方法

     注意傳入的UIEdgeInsetsInsetRect(bounds, self.edgeInsets),bounds是真正的繪圖區域

     */

    CGRect rect = [super textRectForBounds:UIEdgeInsetsInsetRect(bounds,

                                                                 self.edgeInsets) limitedToNumberOfLines:numberOfLines];

    //根據edgeInsets,修改繪制文字的bounds

    rect.origin.x -= self.edgeInsets.left;

    rect.origin.y -= self.edgeInsets.top;

    rect.size.width += self.edgeInsets.left + self.edgeInsets.right;

    rect.size.height += self.edgeInsets.top + self.edgeInsets.bottom;

    return rect;

}

 

//繪制文字

- (void)drawTextInRect:(CGRect)rect

{

    //令繪制區域為原始區域,增加的內邊距區域不繪制

    [super drawTextInRect:UIEdgeInsetsInsetRect(rect, self.edgeInsets)];

}

 

@end

解決方案

為了解決這個問題,設計SFLabel如下,繼承自UILabel

 

將UIlabel的類型改為SFLabel,看看現在效果是否如第二幅圖😊。

注意事項

  • 通過SFLabel中的方法修改UILabel的內邊距最好只修改上下內邊距,通過系統NSMutableParagraphStyle可以修改左邊內邊距;
  • 通過boundingRectWithSize:options:attributes:context:計算SFLabel內容計算出的區域仍然是與直接使用UILabel的結果一樣,因此需要小心使用,可以在boundingRectWithSize:options:attributes:context:基礎上根據edgeInsets進行修正。

SFLabel源碼


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM