zoukankan      html  css  js  c++  java
  • 计算富文本的高度

    计算富文本的高度 在app中我们最常见的就是把一段文字要设置行间距、所占宽度、字体大小。并且这些都是我们来计算富文本高度和设置富文本样式必须的元素。当然可能还有首行缩进等元素。我们这里就不考虑了。

    设置富文本显示

    这里我使用的对NSString的延展封装了一个工具类,方便快捷好用、还好学。


    延展结构图.png

    设置段落样式(不考虑首行缩进情况)

    /**
     *  设置段落样式
     *
     *  @param lineSpacing 行高
     *  @param textcolor   字体颜色
     *  @param font        字体
     *
     *  @return 富文本
     */
    -(NSAttributedString *)stringWithParagraphlineSpeace:(CGFloat)lineSpacing
                                               textColor:(UIColor *)textcolor
                                                textFont:(UIFont *)font {
        // 设置段落
        NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.lineSpacing = lineSpacing;
        // NSKernAttributeName字体间距
        NSDictionary *attributes = @{ NSParagraphStyleAttributeName:paragraphStyle,NSKernAttributeName:@1.5f};
        NSMutableAttributedString * attriStr = [[NSMutableAttributedString alloc] initWithString:self attributes:attributes];
        // 创建文字属性
        NSDictionary * attriBute = @{NSForegroundColorAttributeName:textcolor,NSFontAttributeName:font};
        [attriStr addAttributes:attriBute range:NSMakeRange(0, self.length)];
        return attriStr;
    }

    计算富文本字体高度

    /**
     *  计算富文本字体高度
     *
     *  @param lineSpeace 行高
     *  @param font       字体
     *  @param width      字体所占宽度
     *
     *  @return 富文本高度
     */
    -(CGFloat)getSpaceLabelHeightwithSpeace:(CGFloat)lineSpeace withFont:(UIFont*)font withWidth:(CGFloat)width {
        NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
        //    paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
        /** 行高 */
        paraStyle.lineSpacing = lineSpeace;
        // NSKernAttributeName字体间距
        NSDictionary *dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@1.5f
                              };
        CGSize size = [self boundingRectWithSize:CGSizeMake(width,MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
        return size.height;
    }

    各位看官来看看怎么使用滴

    最多的场景就是我们要算出这个富文本的高度去做一些事情。我们这里算出富文本的高度来让他充分的显示完成

    设置一个富文本
    // 没有设置Frma,我们要根据他的自身的大小来设置
    UILabel *label=[[UILabel alloc]init];
    label.backgroundColor=[UIColor grayColor];
    label.numberOfLines=0;
    [self.view addSubview:label];
    NSString * conts = @"公元前3000年,印度河流域的居民的数字使用就已经比较普遍,居民们采用了十进位制的计算法。到吠陀时代(公元前1500~公元前500年),雅利安人已意识到数字在生产活动和日常生活中的作用,创造了一些简单的、不完全的数字。
    
    公元前3世纪,印度出现了整套的数字,但各地的写法不一,其中最典型的是婆罗门式,它的独到之处就是从1~9每个数都有专用符号,现代数字就是从它们中脱胎而来的。
    当时,“0”还没有出现。到了笈多时代(300~500年)才有了“0”,叫“舜若”(shunya),表示方式是一个黑点“●”,后来衍变成“0”。
    这样,一套完整的数字便产生了。这项劳动创作也对世界文化做出了巨大的贡献。lll";
    
    //设置富文本显示,
    label.attributedText = [conts stringWithParagraphlineSpeace:6 textColor:[UIColor redColor] textFont:[UIFont systemFontOfSize:15]];
    计算富文本的高度,并设置其高度
    // 计算富文本的高度
    CGFloat contHeight = [conts getSpaceLabelHeightwithSpeace:6 withFont:[UIFont systemFontOfSize:15] withWidth:300];
    label.frame = CGRectMake(0, 50, 300, contHeight);
  • 相关阅读:
    MR架构
    概念词汇
    数仓项目06:DWD层
    Informatic学习总结_day03
    oracle_创建和管理表
    oracle_使用子查询创建表
    oracle数据类型
    文本变语音引擎 ekho
    [LNOI2014]LCA
    POJ 2942 Knights of the Round Table
  • 原文地址:https://www.cnblogs.com/pioneerMax/p/5987765.html
Copyright © 2011-2022 走看看