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语言-数组指针
    关于知网文章下载
    SpringBoot静态资源文件存放位置
    ArchLinux安装的vscode修改插件源
    I/O复用的高级应用三:同时处理TCP和UDP服务
    Windows查看动态库/静态库位数的方法
    VS 窗口程序没有输出时
    Windows下编译Boost
    VS设置C++代码注释模板
    No Target Architecture
  • 原文地址:https://www.cnblogs.com/Ganggang888/p/9638603.html
Copyright © 2011-2022 走看看