ios 给多行label增加间距
UILabel+Extension.h
import <UIKit/UIKit.h>
@interface UILabel (Extension)
//竖直列
- (void)setColumnSpace:(CGFloat)columnSpace;
//横行 - (void)setRowSpace:(CGFloat)rowSpace;
@end
UILabel+Extension.m
import "UILabel+Extension.h"
import <CoreText/CoreText.h>
@implementation UILabel (Extension)
-
(void)setColumnSpace:(CGFloat)columnSpace
{
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
//调整间距
[attributedString addAttribute:(__bridge NSString *)kCTKernAttributeName value:@(columnSpace) range:NSMakeRange(0, [attributedString length])];
self.attributedText = attributedString;
} -
(void)setRowSpace:(CGFloat)rowSpace
{
self.numberOfLines = 0;
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
//调整行距
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = rowSpace;
paragraphStyle.baseWritingDirection = NSWritingDirectionLeftToRight;
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [self.text length])];
self.attributedText = attributedString;
}
@end