加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift HTML富文本显示

发布时间:2020-12-14 06:17:04 所属栏目:百科 来源:网络整理
导读:iOS平台下更灵活 override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view,typically from a nib. var strHtml = "htmlbody Hello span style="color:#0f0;font-size:30px;"It is me./span" strHtml += "br

iOS平台下更灵活

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view,typically from a nib.
        var strHtml = "<html><body> Hello <span style="color:#0f0;font-size:30px;">It is me.</span>"
        strHtml += "<br/> <font size="13" color="red">Here you are,Here we are!</font>;</body></html>";
        
        let label:UILabel = UILabel()
        label.text = strHtml
        label.numberOfLines = 0 //Line break when the current line is full display.
        label.lineBreakMode = NSLineBreakMode.byClipping;//Tips:Supported six types.
        
        do{
            let srtData= strHtml.data(using: String.Encoding.unicode,allowLossyConversion: true)!
            let strOptions = [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType]//Tips:Supported four types.
            let attrStr = try NSAttributedString(data: srtData,options: strOptions,documentAttributes: nil)
            label.attributedText = attrStr
        }catch let error as NSError {
            print(error.localizedDescription)
        }

        label.frame = CGRect(x:self.view.frame.origin.x,y:self.view.frame.origin.y,width:self.view.frame.size.width,height:self.view.frame.size.height);
        self.view.addSubview(label)
    }
android平台下更直接
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String strHtml = "<html><body> Hello,<span style=color:#0f0;font-size:30;>It is me.</span>";
        strHtml += "<br/> <font size=13 color=red>Here you are,Here we are!</font>;</body></html>";

        TextView textView = (TextView)findViewById(R.id.text);
        textView.setText(Html.fromHtml(strHtml));
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读