[英]Swift - Font size increases when converting html string to attributed string and back again
I have a textView that allow user to enter NSAttributedString, convert into html string and then store into database. 我有一个textView,允许用户输入NSAttributedString,转换为html字符串,然后存储到数据库中。 I also have code to convert the html string back and display in textView for editing. 我也有将html字符串转换回并显示在textView中进行编辑的代码。 The conversion codes I have are 我拥有的转换代码是
extension String {
func htmlAttributedString() -> NSAttributedString? {
guard let data = self.data(using: String.Encoding.utf16, allowLossyConversion: false) else { return nil }
guard let html = try? NSMutableAttributedString(
data: data,
options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
documentAttributes: nil) else { return nil }
return html
}
}
extension NSAttributedString {
func htmlString() -> String? {
let documentAttributes = [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType]
do {
let htmlData = try self.data(from: NSMakeRange(0, self.length), documentAttributes:documentAttributes)
if let htmlString = String(data:htmlData, encoding:String.Encoding.utf8) { return htmlString }
}
catch {}
return nil
}
}
However the problem is that every time when I save and display the string, all font size increases. 但是,问题在于,每当我保存并显示字符串时,所有字体大小都会增加。 For example, I have a html string 例如,我有一个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; font: 17.0px '.SF UI Text'; color: #000000}
span.s1 {font-family: '.SFUIText'; font-weight: normal; font-style: normal; font-size: 17.00pt}
</style>
</head>
<body>
<p class="p1"><span class="s1">text</span></p>
</body>
</html>
After I convert it into NSAttributedString and back again, everything else are the same but the css lines are changed to this. 在将其转换为NSAttributedString并再次返回之后,其他所有内容都相同,但是css行更改为此。
<style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; line-height: 28.0px; font: 22.7px '.SF UI Text'; color: #000000; -webkit-text-stroke: #000000}
span.s1 {font-family: '.SFUIText'; font-weight: normal; font-style: normal; font-size: 22.67pt; font-kerning: none}
</style>
Am I missing anything? 我有什么想念的吗? Any helps are appreciated! 任何帮助表示赞赏!
Try this method I found from some help and then converted to Swift 3: 尝试从帮助中找到的此方法,然后将其转换为Swift 3:
func newAttrSize(blockQuote: NSAttributedString) -> NSAttributedString
{
let yourAttrStr = NSMutableAttributedString(attributedString: blockQuote)
yourAttrStr.enumerateAttribute(.font, in: NSMakeRange(0, yourAttrStr.length), options: .init(rawValue: 0)) {
(value, range, stop) in
if let font = value as? UIFont {
let resizedFont = font.withSize(font.pointSize * 0.75)
yourAttrStr.addAttribute(.font, value: resizedFont, range: range)
}
}
return yourAttrStr
}
I got the same problem, the simple solution is you just convert to RTF format instead of HTML. 我遇到了同样的问题,简单的解决方案是您只转换为RTF格式而不是HTML。 It 's worked fine. 一切正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.