【问题标题】:Converting Html string to Attributed string and assigning to textview getting extra space将 Html 字符串转换为属性字符串并分配给 textview 以获得额外的空间
【发布时间】:2020-07-21 21:15:36
【问题描述】:

我花了超过 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

输出截图如下:

我认为我的代码有问题,谁能帮忙。

提前致谢。

【问题讨论】:

  • 哪个额外空间?在重要通知和a点之间?
  • @AjinkyaSharma 第 4 点 - 重要通知 - a.
  • 那是在你的 HTML 中.. 额外的

    删除那个.. 或者玩弄那个..
  • @AjinkyaSharma,我认为 html 的问题与我的代码无关。
  • @AjinkyaSharma,当我在 html 字符串中有图像时,textview 它没有完全显示图像。我已经编辑了我的问题,由于字符数的限制,无法将图像 src 路径添加到 html 内容。请帮帮我。

标签: ios swift uitableview textview


【解决方案1】:

我对你的 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>"

.

StackOverflow“Blockquotes”的示例输出

1.必须开启 GPS 才能定位 距离您的 1 公里范围内的面板 GP 诊所 位置。

2. 您可以根据以下条件进行通配符搜索 “诊所名称”或“道路名称”。

3.这 右上角 显示荧光笔的尖端 一个过滤器来查找另一个面板类型,例如。中医或 专家小组(如适用)。

4。搜索完成后, 点击您想要的面板诊所,您可以

  • 查看诊所营业时间
  • 致电 诊所
  • 获取前往面板诊所的路线(GPS 必须是 已开启)。
  • 5. 如果 没有 GPS 开启,您仍然可以访问 S/no 4a 和 4b 选定的面板诊所。

    重要 通知

  • *最后一次诊所注册 在关闭时间前 30 分钟或更早如果 患者登记数量超过了 主治医生和诊所工作人员可以处理超出 诊所正常营业时间。
  • 附加费将是 在*最后一次诊所或之后对会员进行访问 注册。
  • 营业时间仅供参考。 由于诊所运营,请在来访前致电诊所 营业时间如有更改,恕不另行通知。
  • 模拟器中的示例输出:(请忽略截图背景)

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

    【讨论】:

    • 你做了哪些修改?
    • 您可以查看答案中添加的更新后的 HTML。还有一件快速的事情: 1. 我删除了额外的

      标签。 2. 标签的收盘位置发生了一些变化。
    • 但在 webview 中看起来不错。
    • 我不确定为什么它在 WebView 中是正确的。但是在 TextView 中,额外的空间带有

      标签。我去年遇到了这个问题。
    • 谢谢,我可以将此作为对我的项目经理的参考,html 的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-17
    • 1970-01-01
    相关资源
    最近更新 更多