zoukankan      html  css  js  c++  java
  • UILabel设置行间距和字间距并计算高度-b

    #define UILABEL_LINE_SPACE 6

    #define HEIGHT [ [ UIScreen mainScreen ] bounds ].size.height

    //给UILabel设置行间距和字间距

    -(void)setLabelSpace:(UILabel*)label withValue:(NSString*)str withFont:(UIFont*)font {

        NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStylealloc] init];

        paraStyle.lineBreakMode =NSLineBreakByCharWrapping;

        paraStyle.alignment =NSTextAlignmentLeft;

        paraStyle.lineSpacing = UILABEL_LINE_SPACE; //设置行间距

        paraStyle.hyphenationFactor = 1.0;

        paraStyle.firstLineHeadIndent =0.0;

        paraStyle.paragraphSpacingBefore =0.0;

        paraStyle.headIndent = 0;

        paraStyle.tailIndent = 0;

        //设置字间距 NSKernAttributeName:@1.5f

        NSDictionary *dic =@{NSFontAttributeName:font,NSParagraphStyleAttributeName:paraStyle,NSKernAttributeName:@1.5f
    };

        

        NSAttributedString *attributeStr = [[NSAttributedStringalloc] initWithString:strattributes:dic];

        label.attributedText = attributeStr;

    }

    //计算UILabel的高度(带有行间距的情况)

    -(CGFloat)getSpaceLabelHeight:(NSString*)str withFont:(UIFont*)font withWidth:(CGFloat)width {

        NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStylealloc] init];

        paraStyle.lineBreakMode =NSLineBreakByCharWrapping;

        paraStyle.alignment =NSTextAlignmentLeft;

        paraStyle.lineSpacing = UILABEL_LINE_SPACE;

        paraStyle.hyphenationFactor = 1.0;

        paraStyle.firstLineHeadIndent =0.0;

        paraStyle.paragraphSpacingBefore =0.0;

        paraStyle.headIndent = 0;

        paraStyle.tailIndent = 0;

        NSDictionary *dic =@{NSFontAttributeName:font,NSParagraphStyleAttributeName:paraStyle,NSKernAttributeName:@1.5f
    };

        

        CGSize size = [strboundingRectWithSize:CGSizeMake(width,HEIGHT) options:NSStringDrawingUsesLineFragmentOriginattributes:dic context:nil].size;

        return size.height;

    }

  • 相关阅读:
    cookie设置不能马上返回?
    Firemonkey的旁门左道[八]
    小智慧38
    避免15个浪费时间的坏习惯
    Deep Learning论文笔记之(七)深度网络高层特征可视化
    hdu1024 最大m子序列和
    poj 2472
    poj 2472
    hdu4539 郑厂长系列故事——排兵布阵 + POJ1158 炮兵阵地
    hdu4539 郑厂长系列故事——排兵布阵 + POJ1158 炮兵阵地
  • 原文地址:https://www.cnblogs.com/isItOk/p/5838310.html
Copyright © 2011-2022 走看看