为 UILabel 添加内边距

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

  • 11
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值