• [iOS]如何给Label或者TextView赋HTML数据


    //
    //  ViewController.m
    //  text
    //
    //  Created by 李东旭 on 16/1/22.
    //  Copyright © 2016年 李东旭. All rights reserved.
    //
    
    #import "ViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        
        // 创建textView
        UITextView *textV = [[UITextView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 400)];
        [self.view addSubview:textV];
        
        // 设置textView边框宽度和颜色
        textV.layer.borderWidth = 2.0f;
        textV.layer.borderColor = [UIColor blackColor].CGColor;
        
        // 获取html数据
        NSString *htmlString = @"<h1>Header</h1><h2>Subheader</h2><p>Some<em>text</em></p><img src='http://blogs.babble.com/famecrawler/files/2010/11/mickey_mouse-1097.jpg'width=70 height=100 />";
        
        // 利用可变属性字符串来接收html数据
        NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil];
        
        // 给textView赋值的时候就得用attributedText来赋了
        textV.attributedText = attributedString;
        
    }
    
    
    @end
  • 相关阅读:
    线段树再练习
    SCOI 2014 省选总结
    网络流拓展——最小费用最大流
    【集合!】 20140416 && 20140417集训 总结
    Codeforces Round #215 (Div. 1)
    CDQ分治题目小结
    Codeforces Round #232 (Div. 1)
    Codeforces Round #264 (Div. 2)
    Uva 12361 File Retrieval 后缀数组+并查集
    FFT初步学习小结
  • 原文地址:https://www.cnblogs.com/wangqi1221/p/5240261.html
Copyright © 2020-2023  润新知