iOS Label设置内边距 行间距

一、自定义Label,设置内边距

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface PaddingLabel : UILabel

@end

NS_ASSUME_NONNULL_END
#import "PaddingLabel.h"

@interface PaddingLabel ()

@property (assign, nonatomic) UIEdgeInsets edgeInsets;

@end

@implementation PaddingLabel

- (void)awakeFromNib {
    [super awakeFromNib];
    self.edgeInsets = UIEdgeInsetsMake(5, 10, 5, 10);
}

// 修改绘制文字的区域,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 {
    if (self.text && ![self.text isEqualToString:@""]) {
        //令绘制区域为原始区域,增加的内边距区域不绘制
        [super drawTextInRect:UIEdgeInsetsInsetRect(rect, self.edgeInsets)];
        self.hidden = NO;
    } else {
        [super drawTextInRect:UIEdgeInsetsInsetRect(rect, UIEdgeInsetsZero)];
        self.hidden = YES;
    }
}

@end

二、自定义Label,设置行间距

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface LineSpaceLabel : CommonLabel

@property (nonatomic, assign) CGFloat lineSpace;

@end

NS_ASSUME_NONNULL_END
#import "LineSpaceLabel.h"

@implementation LineSpaceLabel

- (void)setLineSpace:(CGFloat)lineSpace {
    _lineSpace = lineSpace;
    self.attributedText = [self.text attributedStringWithLineSpace:lineSpace];
}

- (void)setText:(NSString *)text {
    self.attributedText = [text attributedStringWithLineSpace:self.lineSpace];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    self.lineSpace = 5;
}

@end

设置NSAttributedString方法,参考https://blog.csdn.net/qq_25639809/article/details/89643192

#pragma mark - NSAttributedString
- (NSAttributedString *)attributedStringWithLineSpace:(CGFloat)lineSpace {
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = lineSpace;
    NSRange range = NSMakeRange(0, self.length);
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:range];
    return attributedString;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值