美文网首页
iOS-段间距

iOS-段间距

作者: 拿铁君 | 来源:发表于2016-06-09 15:32 被阅读0次

    //行间距

    /*

    * space 行间距设置

    * label 原始label

    * string 需要输入的内容

    */

    + (UILabel *)setLineSpace:(CGFloat)space FromLabel:(UILabel *)label withString:(NSString *)string;

    {

    label.numberOfLines = 0;

    NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:string];

    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];

    [paraStyle setLineSpacing:space];

    [attributeString addAttribute:NSParagraphStyleAttributeName value:paraStyle range:NSMakeRange(0, [string length])];

    label.attributedText = attributeString;

    [label sizeToFit];

    return label;

    }

    //自适应高度和高度

    /*

    * string 需要输入的内容

    * label  原始label

    * height 最大高度

    * space  行间距

    */

    + (UILabel *)setAutoFrame:(NSString *)string FromLabel:(UILabel *)label withMaxHeight:(CGFloat)height byLineSpace:(CGFloat)space;

    {

    label.numberOfLines = 0;

    label.lineBreakMode = NSLineBreakByTruncatingTail;

    NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:string];

    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];

    [paraStyle setLineSpacing:space];

    [attributeString addAttribute:NSParagraphStyleAttributeName value:paraStyle range:NSMakeRange(0, [string length])];

    label.attributedText = attributeString;

    CGSize size = [label sizeThatFits:CGSizeMake(label.frame.size.width, height)];

    label.frame = CGRectMake(label.frame.origin.x, label.frame.origin.y, size.width, size.height);

    return label;

    }

    相关文章

      网友评论

          本文标题:iOS-段间距

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