UILabel 的默认显示 为上下剧中紧贴左边绘制文本内容。但是, UI设计难免会设计的文字距离label的边界有一些间距,为了更好的设置label的内边距可以利用UILabel的drawTextInRect:方法在绘制文本的时候添加一个内边距。如:

- (void)drawTextInRect:(CGRect)rect {

// contentInsets为我们设置的内边距 UIEdgeInsets
rect = UIEdgeInsetsInsetRect(rect, self.contentInsets);
[super drawTextInRect:rect];

}

使UILabel在绘制市添加上边距。

这里有一个问题,就是rect 是label的大小/或者文字需要完全显示的大小,如果在这里给rect添加了内边距的处理,就是是绘制区域变小,可能是文本绘制/显示不完全。因此,在绘制文本前还需要对UILabel绘制文本大小做一下调整。这里用到 textRectForBounds:limitedToNumberOfLines: 方法

如:

// 修改绘制文字的区域,contentInsets增加bounds
-(CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines

{
    //设置第一行和最后一行距离label的距离
    CGRect rect = [self textRectForBounds:UIEdgeInsetsInsetRect(bounds,self.contentInsets) limitedToNumberOfLines:numberOfLines];

     //根据edgeInsets,修改绘制文字的bounds
     rect.origin.x -= self.contentInsets.left;

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

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

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

    return rect;
}

完整代码如下:

.h 文件

#import <UIKit/UIKit.h>
@interface UILabel (Insets)

// 文字内边距
@property (nonatomic, assign) UIEdgeInsets contentInsets;

@end

#import <UIKit/UIKit.h>

@interface UILabel (Insets)

// 文字内边距

@property (nonatomic, assign) UIEdgeInsets contentInsets;

@end

.m 文件

#import "UILabel+Insets.h"
#import <objc/runtime.h>

static char kContentInsetsKey;

static char kshowContentInsetsKey;

@implementation UILabel (Insets)

- (void)setContentInsets:(UIEdgeInsets)contentInsets {
    
    objc_setAssociatedObject(self, &kContentInsetsKey, NSStringFromUIEdgeInsets(contentInsets), OBJC_ASSOCIATION_COPY_NONATOMIC);
    
    objc_setAssociatedObject(self, &kshowContentInsetsKey, @YES, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (UIEdgeInsets)contentInsets {
    
    return UIEdgeInsetsFromString(objc_getAssociatedObject(self, &kContentInsetsKey));
}

+ (void)load {
    [super load];

    Method fromdrawTextMethod = class_getInstanceMethod([self class], @selector(drawTextInRect:));
    
    Method todrawTextMethod = class_getInstanceMethod([self class], @selector(gw_drawTextInRect:));
    
    if (!class_addMethod([self class], @selector(drawTextInRect:), method_getImplementation(todrawTextMethod), method_getTypeEncoding(todrawTextMethod))) {
        
        method_exchangeImplementations(fromdrawTextMethod, todrawTextMethod);
    }
    
    Method frometTextRectForBoundsMethod = class_getInstanceMethod([self class], @selector(textRectForBounds:limitedToNumberOfLines:));
    Method toTextRectForBoundsMethod = class_getInstanceMethod([self class], @selector(gw_textRectForBounds:limitedToNumberOfLines:));
    
    if (!class_addMethod([self class], @selector(textRectForBounds:limitedToNumberOfLines:), method_getImplementation(toTextRectForBoundsMethod), method_getTypeEncoding(toTextRectForBoundsMethod))) {
        
        method_exchangeImplementations(frometTextRectForBoundsMethod, toTextRectForBoundsMethod);
    }
    
}

- (void)gw_drawTextInRect:(CGRect)rect {
    
    BOOL show = objc_getAssociatedObject(self, &kshowContentInsetsKey);

    if (show) {
        
        rect = UIEdgeInsetsInsetRect(rect, self.contentInsets);
    }
    
    [self gw_drawTextInRect:rect];
}

// 修改绘制文字的区域,contentInsets增加bounds
-(CGRect)gw_textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines

{
    //设置第一行和最后一行距离label的距离
    CGRect rect = [self gw_textRectForBounds:bounds limitedToNumberOfLines:numberOfLines];
    
    BOOL show = objc_getAssociatedObject(self, &kshowContentInsetsKey);
    if (show) {
     //设置第一行和最后一行距离label的距离 和边距
        rect = [self gw_textRectForBounds:UIEdgeInsetsInsetRect(bounds,self.contentInsets) limitedToNumberOfLines:numberOfLines];

        //根据edgeInsets,修改绘制文字的bounds
        rect.origin.x -= self.contentInsets.left;

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

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

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

    }

    return rect;
}



@end

#import "UILabel+Insets.h"

#import <objc/runtime.h>

static char kContentInsetsKey;

static char kshowContentInsetsKey;

@implementation UILabel (Insets)

- (void)setContentInsets:(UIEdgeInsets)contentInsets {

    objc_setAssociatedObject(self, &kContentInsetsKey, NSStringFromUIEdgeInsets(contentInsets), OBJC_ASSOCIATION_COPY_NONATOMIC);

    objc_setAssociatedObject(self, &kshowContentInsetsKey, @YES, OBJC_ASSOCIATION_COPY_NONATOMIC);

}

- (UIEdgeInsets)contentInsets {

        return UIEdgeInsetsFromString(objc_getAssociatedObject(self, &kContentInsetsKey));

}

+ (void)load {

    [super load];

    Method fromdrawTextMethod = class_getInstanceMethod([self class], @selector(drawTextInRect:));

    Method todrawTextMethod = class_getInstanceMethod([self class], @selector(gw_drawTextInRect:));

        if (!class_addMethod([self class], @selector(drawTextInRect:), method_getImplementation(todrawTextMethod), method_getTypeEncoding(todrawTextMethod))) {

        method_exchangeImplementations(fromdrawTextMethod, todrawTextMethod);

    }

        Method frometTextRectForBoundsMethod = class_getInstanceMethod([self class], @selector(textRectForBounds:limitedToNumberOfLines:));

    Method toTextRectForBoundsMethod = class_getInstanceMethod([self class], @selector(gw_textRectForBounds:limitedToNumberOfLines:));

    if (!class_addMethod([self class], @selector(textRectForBounds:limitedToNumberOfLines:), method_getImplementation(toTextRectForBoundsMethod), method_getTypeEncoding(toTextRectForBoundsMethod))) {

                method_exchangeImplementations(frometTextRectForBoundsMethod, toTextRectForBoundsMethod);

    }

}

- (void)gw_drawTextInRect:(CGRect)rect {

        BOOL show = objc_getAssociatedObject(self, &kshowContentInsetsKey);

    if (show) {

          rect = UIEdgeInsetsInsetRect(rect, self.contentInsets);

    }

        [self gw_drawTextInRect:rect];

}

// 修改绘制文字的区域,contentInsets增加bounds

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

{

    //设置第一行和最后一行距离label的距离

    CGRect rect = [self gw_textRectForBounds:bounds limitedToNumberOfLines:numberOfLines];

    BOOL show = objc_getAssociatedObject(self, &kshowContentInsetsKey);

    if (show) {

     //设置第一行和最后一行距离label的距离 和边距

        rect = [self gw_textRectForBounds:UIEdgeInsetsInsetRect(bounds,self.contentInsets) limitedToNumberOfLines:numberOfLines];

        //根据edgeInsets,修改绘制文字的bounds

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

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

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

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

    }

    return rect;

}

@end

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐