zoukankan      html  css  js  c++  java
  • (转)iOS学习——UIlabel设置行间距和字间距

      在iOS开发中经常会用到UIlabel来展示一些文字性的内容,但是默认的文字排版会觉得有些挤,为了更美观也更易于阅读我们可以通过某些方法将UIlabel的行间距和字间距按照需要调节。

      比如一个Label的默认间距效果是这样:

    然后用一个封装起来的Category来调整这部分文字的行间距,其中5.0就是我自定义的文字间距:

    [UILabel changeLineSpaceForLabel:cell.describeLabel WithSpace:5.0];

    调整后的效果是这样的:

    这是一个UILabel 的Category,他的内部实现是这样的:

    UILabel+ChangeLineSpaceAndWordSpace.h

    #import <UIKit/UIKit.h>
    
    @interface UILabel (ChangeLineSpaceAndWordSpace)
    
    /**
     *  改变行间距
     */
    + (void)changeLineSpaceForLabel:(UILabel *)label WithSpace:(float)space;
    
    /**
     *  改变字间距
     */
    + (void)changeWordSpaceForLabel:(UILabel *)label WithSpace:(float)space;
    
    /**
     *  改变行间距和字间距
     */
    + (void)changeSpaceForLabel:(UILabel *)label withLineSpace:(float)lineSpace WordSpace:(float)wordSpace;
    
    @end

    UILabel+ChangeLineSpaceAndWordSpace.m

    #import "UILabel+ChangeLineSpaceAndWordSpace.h"
    
    @implementation UILabel (ChangeLineSpaceAndWordSpace)
    
    + (void)changeLineSpaceForLabel:(UILabel *)label WithSpace:(float)space {
        NSString *labelText = label.text;
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText];
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle setLineSpacing:space];
        [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
        label.attributedText = attributedString;
        [label sizeToFit];
    }
    
    + (void)changeWordSpaceForLabel:(UILabel *)label WithSpace:(float)space {
        NSString *labelText = label.text;
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText attributes:@{NSKernAttributeName:@(space)}];
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
        label.attributedText = attributedString;
        [label sizeToFit];
    }
    
    + (void)changeSpaceForLabel:(UILabel *)label withLineSpace:(float)lineSpace WordSpace:(float)wordSpace {
        NSString *labelText = label.text;
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText attributes:@{NSKernAttributeName:@(wordSpace)}];
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle setLineSpacing:lineSpace];
        [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
        label.attributedText = attributedString;
        [label sizeToFit];
    }
    
    @end
  • 相关阅读:
    别让猴子翻到背上
    python生成二维码
    50条经典爱情观
    智力测试题
    SQL数据库优化
    递归函数实现二分查找法
    软件开发类别
    递归函数的深度问题
    UVALive
    UVALive
  • 原文地址:https://www.cnblogs.com/mukekeheart/p/10215176.html
Copyright © 2011-2022 走看看