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);
    }
    
  • 相关阅读:
    C#遍历DataSet中数据的几种方法总结
    angularjs作用域
    SQL 语句日期用法及函数
    Sublime Text3 快捷键汇总
    AngularJS 最常用的功能
    C#中如何排除/过滤/清空/删除掉字符串数组中的空字符串
    AngularJS的指令用法
    [WCF REST] Web消息主体风格(Message Body Style)
    泛型
    语法补充
  • 原文地址:https://www.cnblogs.com/Ganggang888/p/9638603.html
Copyright © 2011-2022 走看看