美文网首页
ios 给多行label增加间距

ios 给多行label增加间距

作者: 王家小雷 | 来源:发表于2019-11-29 15:31 被阅读0次

    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

    相关文章

      网友评论

          本文标题:ios 给多行label增加间距

          本文链接:https://www.haomeiwen.com/subject/pvrgwctx.html