gpt4 book ai didi

swift - 无法使用 NSHTMLTextDocumentType 读取 标签

转载 作者:行者123 更新时间:2023-11-28 12:04:24 25 4
gpt4 key购买 nike

我的代码无法读取 <strong>标记,我无法将其设为粗体:

let txt = "`<p><em>The first day of your <strong>Kerala tour itinerary</strong> starts with a trip along the winding roads to the beautiful hill station of Munnar</em></p>\r\n<p>`Your holiday to the mesmerizing Munnar starts with `a&nbsp;`transfer from Cochin. Enjoy the picturesque hill-station as you drive through the serpentine roads surrounded by lush green hills. En route, visit the beautiful Cheeyappara, Valara, and Attukadu waterfalls and the spice plantations, as part of your `<strong>3 day Kerala tourism package itinerary</strong>.` Upon arrival, check-in to the hotel and relax for a while. Later, enjoy local sightseeing and marvel at the beauty all around. Mark an end to the first day of `<strong>Munnar tour package</strong>` with a comfortable overnight stay at the hotel.`&nbsp;</p>\r\n<p><strong>Distance from Cochin to Munnar:</strong>&nbsp;130 km (approx.)<br /><strong>Travel Time:</strong>&nbsp;4 hours (approx.)&nbsp;</p>`"




let dataValue = txt.data(using: String.Encoding.unicode,
allowLossyConversion: true)!
let optionaArr = [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType]

let attrStr = try! NSAttributedString(data: dataValue,
options: optionaArr,
documentAttributes: nil)
txtView.attributedText = attrStr

除了标签,我可以读取所有标签,例如 <p>, <em>和所有。提前致谢。

最佳答案

下面的代码适用于我(swift 4.1 和 Xcode 9.3)

let txt = "`<p><em>The first day of your <strong>Kerala tour itinerary</strong> starts with a trip along the winding roads to the beautiful hill station of Munnar</em></p>\r\n<p>`Your holiday to the mesmerizing Munnar starts with `a&nbsp;`transfer from Cochin. Enjoy the picturesque hill-station as you drive through the serpentine roads surrounded by lush green hills. En route, visit the beautiful Cheeyappara, Valara, and Attukadu waterfalls and the spice plantations, as part of your `<strong>3 day Kerala tourism package itinerary</strong>.` Upon arrival, check-in to the hotel and relax for a while. Later, enjoy local sightseeing and marvel at the beauty all around. Mark an end to the first day of `<strong>Munnar tour package</strong>` with a comfortable overnight stay at the hotel.`&nbsp;</p>\r\n<p><strong>Distance from Cochin to Munnar:</strong>&nbsp;130 km (approx.)<br /><strong>Travel Time:</strong>&nbsp;4 hours (approx.)&nbsp;</p>`"

let dataValue = txt.data(using: String.Encoding.unicode,
allowLossyConversion: true)!

let attrStr = try! NSAttributedString(data: dataValue,
options: [.documentType: NSAttributedString.DocumentType.html],
documentAttributes: nil)
txtView.attributedText = attrStr



模拟器截图:

enter image description here

关于swift - 无法使用 NSHTMLTextDocumentType 读取 <strong> 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49690879/

25 4 0