为 UILabel 添加内边距

2024-06-16 21:52
文章标签 uilabel 内边

本文主要是介绍为 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

这篇关于为 UILabel 添加内边距的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1067649

相关文章

div+css之 css框模型 内边距 边框 外边距 外边距合并

1、CSS 框模型: 术语翻译 ·        element : 元素。 ·        padding : 内边距,也有资料将其翻译为填充。 ·        border : 边框。 ·        margin : 外边距,也有资料将其翻译为空白或空白边。     元素框的最内部分是实际的内容,直接包围内容的是内边距。内边距呈现了元素的背景。内边距的边缘是边框。边

对UILabel添加UIMenuController扩展

一、UIMenuController认识 1、默认情况下,UITextView / UITextFiled / UIWebView 都有苹果自带的有UIMenuController功能 二、对UILabel添加UIMenuController扩展 1、新建一个SSCopyLabel,继承UIlabel,.m文件如下: #import "SSCopyLabel.h"@implementati

iOS开发基础控件--UILabel

UILabel 的常见属性和方法: //创建UIlabel对象 UILabel* label = [[UILabel alloc] initWithFrame:self.view.bounds]; //设置显示文本 label.text = @"This is a UILabel Demo"; //设置阴影 label.shadowColor = [UIColor redColor]

[iOS]修改UILabel的行间距/段间距/缩进

[iOS]修改UILabel的行间距/段间距/缩进 - (void)viewDidLoad {[super viewDidLoad];UILabel *dLabel = _desLab;NSString *dLabelString = dLabel.text;NSMutableAttributedString *attributedString = [[NSMutableAttri

[iOS]UILabel和UIButton添加删除线和下划线

UILabel和UIButton添加删除线和下划线 /*** UILabel、UIButton的删除线/下划线* mark = 0 删除线、= 1 下划线*/- (void)createLineInView:(UIView *)theView Mark:(NSInteger)mark {NSString *tempStr = @"";UIButton *tempBut;if ([[t

[Swift]UITextField在左侧添加内边距或图标

self.field_main.addLeftTextPadding(10) import UIKitextension UITextField {/// 添加左内边距public func addLeftTextPadding(_ blankSize: CGFloat) {let leftView = UIView()leftView.frame = CGRect(x: 0, y: 0, wi

css 内边距 外边距 边框

1.内边距CSS padding 属性定义元素边框与元素内容之间的空白区域。 例子   padding-left:20px; CSS 内边距属性 属性 描述 padding 简写属性。作用是在一个声明中设置元素的所内边距属性。 padding-bottom 设置元素的下内边距。 padding-left 设置元素的左内边距。 padding-right 设置元素的右内边距。 padd

【iOS】UILabel自适应高度和自动换行

代码: [plain]  view plain copy //初始化label   UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,0,0)];   //设置自动行数与字符换行   [label setNumberOfLines:0];   label.lineBreakMode = UILin

iOS UILabel计算高度与宽带

1、在IOS开发中,我们经常需要根据文本的长短动态的设置UILabel的显示位置,这时我们就需要知道UILabel的宽与高。 在IOS6.0以及以前的版本计算UILabel的高度与宽度,在IOS7.0 sizeWithFont被遗弃不在使用。         1、1 定义单行字符串,计算Label的宽度与高度 NSString *content=@"欢迎访问";UILab

Swift UILabel 数字动画效果

昨天去面试 面试官问我 如何实现 label数字由低到高增长。特地整理了一下  用的时候 只要调用这个类 即可实现 在多少s内 有 min - max 的方法 ///自己写的一个类 class UIZJLAnimationLab: UILabel {          ///计时器比 NSTimer精确     var timer:CADisplayLink