ios - 如何使用 HTML 标签在 swift 中显示表格

标签 ios swift

我从 API 获取包含 html 标签的文本。我的意思是如何在 UILabel 中显示它?我尝试使用 NSAttributedString,但不是我需要的。

以下是我的回复

<div id="Details" class="a-section a-spacing-extra-large">
<div id="feature_div" class="feature" data-feature-name="Description" data-cel-widget="feature_div">
<div id="feature_div" class="a-row feature" data-feature-name="productDescription" data-template-name="productDescription" data-cel-widget="productDescription_feature_div">
<div id="productDescription" class="a-section a-spacing-small">
<p>Manual caulking guns have a thrust ratio; the higher the thrust ratio, the easier it is to dispense the caulk, sealant, or adhesive.</p>
<div></div>
</div>
</div>
</div>
</div>
<div id="p13n-m-desktop-dp-sims_session-similarities-sims-feature-4" class="celwidget" data-cel-widget="desktop-dp-sims_session-similarities-sims-feature-4">
<div class="a-section similarities-widget sims-carousel-holder" data-similarity-type="desktop-dp-sims_session-similarities"></div>
</div>)
"Hi"
Optional(<ul class="a-unordered-list a-vertical a-spacing-none">
<li><span class="a-list-item">dispesing rebaring</span></li>
<li><span class="a-list-item">Easy to handle</span></li>
<li><span class="a-list-item">frame caulking gun</span></li>
<li><span class="a-list-item">requires less force </span></li>
<li><span class="a-list-item">to help prevent dripping</span></li>
</ul>
<div>
<table id="product-specification-table" class="a-keyvalue">
<tbody>
<tr><th class="a-span4 a-text-right">Brand Name</th>
<td>ICFS359</td>
</tr>
<tr><th class="a-span4 a-text-right">Colour</th>
<td>Red</td>
</tr>
<tr><th class="a-span4 a-text-right">Included Components</th>
<td>IDispenser Caulking Gun</td>
</tr>
<tr><th class="a-span4 a-text-right">Material Type</th>
<td>Steel</td>
</tr>
<tr><th class="a-span4 a-text-right">Number of Items</th>
<td>1</td>
</tr>
<tr><th class="a-span4 a-text-right">Number</th>
<td>ICFSDM</td>
</tr>
<tr><th class="a-span4 a-text-right">Size</th>
<td>Standard</td>
</tr>
</tbody>
</table>
</div>)

我试过这个

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)
        } catch {
            return NSAttributedString()
        }
    }
    var htmlToString: String {
        return htmlToAttributedString?.string ?? ""
    }
}

但我想以下面的格式显示我的回复,

品牌名称ICFS359

容量10

请帮忙

最佳答案

通过您提供的上述扩展,您可以使用 UILabel 假设您有一个包含响应 html 值的字符串,如下所示:

let htmlString = "<div id="Details">".htmlToAttributedString
label.attributedText = htmlString

或者因为我在该响应中看到一个可选,而不是强制展开它或强制转换为字符串:

if let htmlString = response["value"]  {
    label.attributedText = htmlString.htmlToAttributedString
}

它看起来像这样: https://ibb.co/zFQLqmp 如果这就是您正在寻找的内容

关于ios - 如何使用 HTML 标签在 swift 中显示表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58073828/

相关文章:

ios - 需要解释 iOS 中的@property/@synthesize

ios - CLLocation distanceFromLocation 返回错误答案,然后返回正确答案

ios - 调用 NSCashe 不起作用

ios - UITableHeaderView autoLayout 中的 UILabel 不适用于 iPhone 6

ios - 如何检查用户 JID 是否已在 XMPPFrameWork 中使用

iphone - 如何制作一个显示下拉菜单的按钮?

ios - 排序描述符在 ios 中不起作用

swift - 如何在 swift 中使用 guard 而不是 if

ios - TapGesture 在 tabBar 下不起作用

ios - “滑动时隐藏导航栏”永远不会返回