gpt4 book ai didi

html - NSAttributedString 到没有 CSS 的 HTML 字符串

转载 作者:可可西里 更新时间:2023-11-01 13:26:11 26 4
gpt4 key购买 nike

我正在尝试将 HTML 转换为 NSAttributedString 格式并返回。从 HTML 转换工作正常,但是转换回 HTML 给了我一种无法发送到后端的格式:

<!DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv=Content-Style-Type" content="text/css">
<title></title>
<meta name="Generator" content="Cocoa HTML Writer">
<style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; line-height: 15.0px; font: 16.0px 'Times New Roman'; color: #000000; -webkit-text-stroke: #000000}
span.s1 {font-family: 'Times New Roman'; font-weight: normal; font-style: normal; font-size: 16.00pt; font-kerning: none}
span.s2 {font-family: 'TimesNewRomanPS-BoldMT'; font-weight: bold; font-style: normal; font-size: 16.00pt; font-kerning: none}
span.s3 {font-family: 'TimesNewRomanPS-ItalicMT'; font-weight: normal; font-style: italic; font-size: 16.00pt; font-kerning: none}
span.s4 {font-family: 'TimesNewRomanPS-BoldItalicMT'; font-weight: bold; font-style: italic; font-size: 16.00pt; font-kerning: none}
</style>
</head>
<body>
<p class="p1"><span class="s1">vanlig text </span><span class="s2">bold</span><span class="s1"> </span><span class="s3">italic</span><span class="s1"> </span><span class="s4">bold_italic asd</span></p>
</body>
</html>

我需要发送给我的服务器的字符串/格式不能有 css,需要更像这样:(没有 CSS)

<html>
<body>
<p>vanlig text <b>bold</b> <i>italic</i><b><i>bold_italic asd</i></b>
</p>
</body>
</html>

我试过这个解决方案但没有成功:NSAttribute string to HTML我用 css 保留格式。

到目前为止我的代码:

extension NSAttributedString
{
var htmlString : String
{
let documentAttributes = [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType]
if let htmlData = try? self.dataFromRange(NSMakeRange(0, self.length), documentAttributes:documentAttributes),
let htmlString = String(data:htmlData, encoding:NSUTF8StringEncoding) {
return htmlString
}
return ""
}

func attributedStringWithNoTrailingNewLines() -> NSMutableAttributedString
{
let mutableAttributedString = NSMutableAttributedString(attributedString: self)
let nsString = string as NSString
let lastWhitespaceOrNewlineRange = nsString.rangeOfCharacterFromSet(NSCharacterSet.whitespaceAndNewlineCharacterSet(), options: .BackwardsSearch)
if lastWhitespaceOrNewlineRange.length != 0 && NSMaxRange(lastWhitespaceOrNewlineRange) == self.length
{
mutableAttributedString.replaceCharactersInRange(lastWhitespaceOrNewlineRange, withString: "")
}
return mutableAttributedString
}
}

extension String
{
var attributedString : NSAttributedString?
{
if let data = self.dataUsingEncoding(NSUTF8StringEncoding) {
let options = [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType]
let attrStr = try? NSAttributedString(data: data, options: options, documentAttributes: nil)
return attrStr
}
return nil
}
}

最佳答案

我没有用于解决它的确切代码,但我通过迭代 nsfontatributes 并手动添加支持的 html 标签解决了它。在 NSAttributedString 扩展中是这样的:

enumerateAttribute(NSFontAttributeName, inRange: NSMakeRange(0, length), options: NSAttributedStringEnumerationOptions(rawValue: 0)) { (value, range, stop) in
if let font = value as? UIFont {
var stringInRange = string.substringInRange(range)
if font.isBold {
stringInRange = "<b>\(stringInRange)</b>"
}
if font.isItalic {
stringInRange = "<i>\(stringInRange)</i>"
}
// ...
}
}

关于html - NSAttributedString 到没有 CSS 的 HTML 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37962782/

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