gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 05:26:54 25 4
gpt4 key购买 nike

我从 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com