将 Html 字符串转换为属性字符串并分配给 textview 获得额外的 space

Converting Html string to Attributed string and assigning to textview getting extra space

我花了 8 个多小时,但无法解决我的问题。 我的代码如下:

let sampleHTML = "<p><span style=\"background-color: rgb(255, 255, 255);\"><font color=\"#000000\" style=\"\" size=\"1\"><b style=\"\">1. GPS must be turned on<\/b>&nbsp;to locate panel GP clinics within 1 km from your location.<\/font><\/span><\/p><p class=\"p1\" style=\"margin-right: 0px; margin-left: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: Menlo;\"><span style=\"background-color: rgb(255, 255, 255);\"><font color=\"#000000\" size=\"1\">2.&nbsp;You can do a wildcard search based on “Clinic Name” or “Road Name”.<\/font><\/span><\/p><p class=\"p1\" style=\"margin-right: 0px; margin-left: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: Menlo;\"><span style=\"background-color: rgb(255, 255, 255);\"><font color=\"#000000\" size=\"1\">3. The top right corner&nbsp;shows the tip of a highlighter which is a <b>Filter<\/b>&nbsp;to find another Panel type eg. TCM or Specialist Panel (where applicable).<\/font><\/span><\/p><p class=\"p1\" style=\"margin-right: 0px; margin-left: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: Menlo;\"><span style=\"background-color: rgb(255, 255, 255);\"><font color=\"#000000\" size=\"1\">4. When the search is done, click on your desired panel clinic and you can<\/font><\/span><\/p><blockquote style=\"margin: 0 0 0 40px; border: none; padding: 0px;\"><p class=\"p2\" style=\"margin-right: 0px; margin-left: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: Menlo;\"><span style=\"background-color: rgb(255, 255, 255);\"><b><font color=\"#000000\" size=\"1\">(a) View Clinic Operating hours<\/font><\/b><\/span><\/p><\/blockquote><blockquote style=\"margin: 0 0 0 40px; border: none; padding: 0px;\"><p class=\"p1\" style=\"margin-right: 0px; margin-left: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: Menlo;\"><b style=\"background-color: rgb(255, 255, 255);\"><font color=\"#000000\" size=\"1\">(b) Call the clinic<\/font><\/b><\/p><\/blockquote><blockquote style=\"margin: 0 0 0 40px; border: none; padding: 0px;\"><p class=\"p1\" style=\"margin-right: 0px; margin-left: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: Menlo;\"><b style=\"background-color: rgb(255, 255, 255);\"><font color=\"#000000\" size=\"1\">(c) Get Direction to the panel clinic (GPS must be turned on)<\/font><\/b><\/p><\/blockquote><p class=\"p1\" style=\"margin-right: 0px; margin-left: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: Menlo;\"><span style=\"background-color: rgb(255, 255, 255);\"><font color=\"#000000\" size=\"1\">5.If <b>GPS is not turned on<\/b>, you can still access to 4a and 4b for your selected panel clinic.<\/font><\/span><\/p><p class=\"p1\" style=\"margin-right: 0px; margin-left: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: Menlo;\"><b><i style=\"background-color: rgb(255, 255, 255);\"><font color=\"#000000\" size=\"1\">Important Notice<\/font><\/i><\/b><\/p><p class=\"p1\" style=\"margin-right: 0px; margin-left: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: Menlo;\"><span style=\"background-color: rgb(255, 255, 255);\"><font color=\"#000000\" size=\"1\">(1) <i>*Last Clinic Registration is<\/i>&nbsp;<b>30 Minutes before closing time or earlier<\/b> if the number of patients’ registration exceeded the capacity that the attending doctor and clinic staff can handle that goes beyond the clinic normal operating hours.<\/font><\/span><\/p><p class=\"p1\" style=\"margin-right: 0px; margin-left: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: Menlo;\"><span style=\"background-color: rgb(255, 255, 255);\"><font color=\"#000000\" size=\"1\">(2) <b>Surcharge<\/b> will be imposed on members for visit <i>on or after *Last Clinic Registration<\/i>.<\/font><\/span><\/p><p class=\"p1\" style=\"margin-right: 0px; margin-left: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: Menlo;\"><span style=\"background-color: rgb(255, 255, 255);\"><font color=\"#000000\" size=\"1\">(3) <b>Operating hours<\/b> are indicative. Please call the clinic before visiting as clinics’ operating hours may change without prior notice.<\/font><\/span><\/p><div><font color=\"rgba(255, 255, 255, 0.850980392156863)\" face=\"Menlo\" size=\"1\"><br><\/font><\/div><div><font size=\"1\"><img src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAABAAAAAQACAIAAADwf7zUAAAKMWlDQ1BJQ0MgUHIAQAAAAAICAEAAAAACAgBAAAAAAgIAQAAAAAICD\/H8\/yGKggh8zsAAAAAElFTkSuQmCC\"><\/font><font color=\"rgba(255, 255, 255, 0.850980392156863)\" face=\"Menlo\"><span style=\"font-size: 1px;\"><br><\/span><\/font><\/div>"

将 html 转换为属性字符串扩展如下:

    extension String {
    var htmlToAttributedString: NSAttributedString? {
        guard let data = data(using: .utf8) else { return NSAttributedString() }
        do {
            return try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding:String.Encoding.utf8.rawValue], documentAttributes: nil)

            //[.documentType: NSAttributedString.DocumentType.html, .characterEncoding:String.Encoding.utf8.rawValue]
        } catch {
            return NSAttributedString()
        }
    }

    var htmlToString: String {
        return htmlToAttributedString?.string ?? ""
    }   
}

正在将属性文本加载到 textview,如下所示:

cell.txtViewHTML.attributedText = sampleHTML.htmlToAttributedString

输出截图如下:

我认为我的代码有问题,任何人都可以帮忙。

提前致谢。

我对你的HTML代码做了一些修改:

let sampleHTML = "<p><b>1. GPS must be turned on</b>&#160;to locate panel GP clinics within 1 km from your location.</p><p><b>2.</b>&#160;You can do a wildcard search based on &#8220;Clinic Name&#8221; or &#8220;Road Name&#8221;.</p><p><b>3. The top right corner</b>&#160;shows the tip of a highlighter which is a&#160;<b>Filter</b>&#160;to find another Panel type eg. TCM or Specialist Panel (where applicable).</p><p>4. When the search is done, click on your desired panel clinic and you can<ol type=\"a\"><li><b>View Clinic Operating hours</b></li><li><b>Call the clinic</b></li><li><b>Get Direction to the panel clinic (GPS must be turned on).</b></li></ol></p><p><b>5.</b>&#160;If&#160;<b>GPS is not turned on</b>, you can still access to S/no 4a and 4b for your selected panel clinic.</p><h1><p><i><font size=\"3\">Important Notice</font></i></h1><ol type=\"a\"><li><i>*Last Clinic Registration is&#160;</i><b>30 Minutes before closing time or earlier</b>if the number of patients&#8217; registration exceeded the capacity that the attending doctor and clinic staff can handle that goes beyond the clinic normal operating hours.</li><li><b>Surcharge&#160;</b>will be imposed on members for visit&#160;<i>on or after *Last Clinic Registration.</i></li><li><b>Operating hours&#160;</b>are indicative. Please call the clinic before visiting as clinics&#8217; operating hours may change without prior notice.</li></ol></p>"

.

Whosebug 的示例输出 "Blockquotes"

1. GPS must be turned on to locate panel GP clinics within 1 km from your location.

2. You can do a wildcard search based on “Clinic Name” or “Road Name”.

3. The top right corner shows the tip of a highlighter which is a Filter to find another Panel type eg. TCM or Specialist Panel (where applicable).

4. When the search is done, click on your desired panel clinic and you can

  • View Clinic Operating hours
  • Call the clinic
  • Get Direction to the panel clinic (GPS must be turned on).
  • 5. If GPS is not turned on, you can still access to S/no 4a and 4b for your selected panel clinic.

    Important Notice

  • *Last Clinic Registration is 30 Minutes before closing time or earlierif the number of patients’ registration exceeded the capacity that the attending doctor and clinic staff can handle that goes beyond the clinic normal operating hours.
  • Surcharge will be imposed on members for visit on or after *Last Clinic Registration.
  • Operating hours are indicative. Please call the clinic before visiting as clinics’ operating hours may change without prior notice.
  • 模拟器中的示例输出:(请忽略屏幕截图背景)

    如果您在 HTML 部分需要任何帮助,请告诉我。