zoukankan      html  css  js  c++  java
  • lab添加显示HTML数据

    -(void)setHTMLTextToLab:(UILabel *)lab withHTMLStr:(NSString *)htmlStr
    {
        
        //str是要显示的字符串
        NSMutableAttributedString * attrString = [[NSMutableAttributedString alloc] initWithData:[htmlStr dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} documentAttributes:nil error:nil];
        [attrString addAttributes:@{NSBaselineOffsetAttributeName: @(5),//设置基线偏移值,取值为 NSNumber (float),正值上偏,负值下偏,可使UILabel文本垂直居中
                                    NSFontAttributeName:[UIFont systemFontOfSize:14]} range:NSMakeRange(0, attrString.length)];
        
        lab.attributedText = attrString;
        
        
        //计算html字符串高度
        NSMutableAttributedString *htmlString =[[NSMutableAttributedString alloc] initWithData:[htmlStr dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute:[NSNumber numberWithInt:NSUTF8StringEncoding]} documentAttributes:NULL error:nil];
        
        [htmlString addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} range:NSMakeRange(0, htmlString.length)];
        //
        CGSize textSize = [htmlString boundingRectWithSize:(CGSize){kScreenH - 20, CGFLOAT_MAX} options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;
        //
        lab.textAlignment = NSTextAlignmentCenter;
        lab.numberOfLines = 0 ;
        lab.frame = CGRectMake(0, lab.frame.origin.y, [UIScreen mainScreen].bounds.size.width, textSize.height);
    }
    
  • 相关阅读:
    重构二叉树
    LeetCode-Construct Binary Tree from Preorder and Inorder Traversal
    二叉平衡树
    二叉树的三种递归与非递归遍历算法
    Jump Game
    Ubuntu中安装最新 Node.js 和 npm
    ORACLE查看并修改最大连接数
    设计模式之工厂模式
    设计模式之单例模式
    设计模式之模板方法模式
  • 原文地址:https://www.cnblogs.com/Ganggang888/p/9638603.html
Copyright © 2011-2022 走看看