您的位置:首页 > 科技 > 能源 > 为 UILabel 添加内边距

为 UILabel 添加内边距

2024/11/17 4:44:12 来源:https://blog.csdn.net/wang_gwei/article/details/139723966  浏览:    关键词:为 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,修改绘制文字的boundsrect.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,修改绘制文字的boundsrect.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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com