記錄一下之前項目加載 HTML 文字的一些問題
HTML 文字加載 大家在其它的 blog 里都有方法實現(xiàn),我就不贅述了
遇到了一個問題就是 需要將文本文字按大小顯示
需求是這樣的:
image.png
之前代碼中顯示 HTML 文字是先加載 HTML 文字,然后再設置的 font 大小,所以字體大小是一樣的
這樣顯示的:
image.png
但是這樣無法滿足產(chǎn)品的需求,所以要將需要標注的為小號的字體利用 html 文本 <small>##</small>標注起來, 然后再將該段所有 HTML 文本使用 html 字體樣式<font size='4'>##</font>標注起來.
最重要的一步,將本地設置字體大小的代碼刪除掉. 就可以按照產(chǎn)品需求顯示了
image.png
let contentText = "<font color='#E21A43'><big>請確認用戶是老人或小孩鸽粉,一經(jīng)設置不可修改</big></font><br/><br/>設置后嘁酿,用戶僅需支付本單半價費用<br/><br/><font color='#898989'><small>注:小孩可以門店存包柜第三格高度以下為準</small></font>"
contentLabel.font = UIFont.systemFont(ofSize: kRemindTextFont)
if contentText.contains("</font>") {
do{
if contentText.contains("</small>") || contentText.contains("</big>") {
let text = "<font size='4'>" + contentText + "</font>"
contentLabel.attributedText = try NSAttributedString(data: text.data(using: String.Encoding.unicode, allowLossyConversion: true)!, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil)
} else {
contentLabel.attributedText = try NSAttributedString(data: contentText.data(using: String.Encoding.unicode, allowLossyConversion: true)!, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil)
contentLabel.font = UIFont.systemFont(ofSize: kRemindTextFont)
}
}catch let error as NSError {
print(error.localizedDescription)
}
}