Swift 读取 RTF 文件不适用标签
Swift reading RTF file doesn't apply Tags
我正在读取一个 rtf 文件,但输出结果显示包含标签的简单字符串,它没有按应有的 NSAttributedString
显示
这是我的代码
if let rtfPath = Bundle.main.url(forResource: "SampleHTML", withExtension: "rtf") {
do {
let attributedStringWithRtf: NSAttributedString =
try NSAttributedString(url: rtfPath, options: [NSAttributedString.DocumentReadingOptionKey.documentType:
NSAttributedString.DocumentType.rtf], documentAttributes: nil)
}catch let error {
print("Got an error \(error)")
}
}
读取文件后,其属性字符串显示为:
< p > < em>查看以下信息。然后select最佳答案点击提交。回答完毕后,点击下一题继续前进。
.
我在标签中插入了空格以阐明问题,没有空格 Whosebug 将标签应用于文本,我在这里缺少什么?
在 RTF 到 NSAttributedString
之后你得到了(你可以使用 SO 的代码标签来避免 HTML 标签被解释):
<p><em>Review the following information. Then select the best answer and click <strong>Submit</strong>. After answering, click <strong>Next Question</strong> to move forward.</em></p>
所以现在,您有一个 HTML AttributedString。
和 RTF 一样,有一种方法可以从 HTML 字符串中获取 NSAttributedString
。
因此,让我们首先获取 HTML 字符串:let htmlString = attributedStringWithRtf.string
.
对于其余部分,该方法几乎与使用的方法相同,但有所不同:NSAttributedString.DocumentType.html
而不是 NSAttributedString.DocumentType.rtf
(似乎很明显),并且有一个初始化 data
而不是路径, 所以你只需要使用 let htmlData = htmlString.data(using:.utf8)
所以代码:
let rtfPath = Bundle.main.url(forResource: "SampleHTML", withExtension: "rtf")
let attributedStringWithRtf: NSAttributedString = NSAttributedString(url: rtfPath, options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.rtf], documentAttributes: nil)
let htmlString = attributedStringWithRtf.string
let htmlData = htmlString.data(using:.utf8)
let attributedString: NSAttributedString = NSAttributedString(data: htmlData, options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil)
注意:我不是故意使用if let
、try
/catch
因为你已经正确使用它们了,我'我对您的问题背后的逻辑以及如何摆脱它更感兴趣。
现在,经过评论讨论:
您将 HTML 文本(即带有 HTML 标签)保存到一个 RTF 文件中。
显然,这太过分了,因为正如您在我给您的解决方案中看到的那样,进行了两次转换以获得最终的 NSAttributedString
.
所以,我要做的是要么完全使用 RTF 文件,然后将 RTF 标签设置为 <p>
、<em>
、<strong>
等,而不是 HTML。
在简单的 .txt
.
中保存 HTML 字符串(带有 HTML 标签)
比如我用TextEdit.app保存了一个RTF文件,实际内容是:
{\rtf1\ansi\ansicpg1252\cocoartf1504\cocoasubrtf830
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
{\*\expandedcolortbl;;}
\paperw11900\paperh16840\margl1440\margr1440\vieww10800\viewh8400\viewkind0
\pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\pardirnatural\partightenfactor0
\f0\fs24 \cf0 myText}
我刚刚写了 myText
,RTF 添加了很多 if own 标签。
我正在读取一个 rtf 文件,但输出结果显示包含标签的简单字符串,它没有按应有的 NSAttributedString
显示
这是我的代码
if let rtfPath = Bundle.main.url(forResource: "SampleHTML", withExtension: "rtf") {
do {
let attributedStringWithRtf: NSAttributedString =
try NSAttributedString(url: rtfPath, options: [NSAttributedString.DocumentReadingOptionKey.documentType:
NSAttributedString.DocumentType.rtf], documentAttributes: nil)
}catch let error {
print("Got an error \(error)")
}
}
读取文件后,其属性字符串显示为:
< p > < em>查看以下信息。然后select最佳答案点击提交。回答完毕后,点击下一题继续前进。
我在标签中插入了空格以阐明问题,没有空格 Whosebug 将标签应用于文本,我在这里缺少什么?
在 RTF 到 NSAttributedString
之后你得到了(你可以使用 SO 的代码标签来避免 HTML 标签被解释):
<p><em>Review the following information. Then select the best answer and click <strong>Submit</strong>. After answering, click <strong>Next Question</strong> to move forward.</em></p>
所以现在,您有一个 HTML AttributedString。
和 RTF 一样,有一种方法可以从 HTML 字符串中获取 NSAttributedString
。
因此,让我们首先获取 HTML 字符串:let htmlString = attributedStringWithRtf.string
.
对于其余部分,该方法几乎与使用的方法相同,但有所不同:NSAttributedString.DocumentType.html
而不是 NSAttributedString.DocumentType.rtf
(似乎很明显),并且有一个初始化 data
而不是路径, 所以你只需要使用 let htmlData = htmlString.data(using:.utf8)
所以代码:
let rtfPath = Bundle.main.url(forResource: "SampleHTML", withExtension: "rtf")
let attributedStringWithRtf: NSAttributedString = NSAttributedString(url: rtfPath, options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.rtf], documentAttributes: nil)
let htmlString = attributedStringWithRtf.string
let htmlData = htmlString.data(using:.utf8)
let attributedString: NSAttributedString = NSAttributedString(data: htmlData, options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil)
注意:我不是故意使用if let
、try
/catch
因为你已经正确使用它们了,我'我对您的问题背后的逻辑以及如何摆脱它更感兴趣。
现在,经过评论讨论:
您将 HTML 文本(即带有 HTML 标签)保存到一个 RTF 文件中。
显然,这太过分了,因为正如您在我给您的解决方案中看到的那样,进行了两次转换以获得最终的 NSAttributedString
.
所以,我要做的是要么完全使用 RTF 文件,然后将 RTF 标签设置为 <p>
、<em>
、<strong>
等,而不是 HTML。
在简单的 .txt
.
比如我用TextEdit.app保存了一个RTF文件,实际内容是:
{\rtf1\ansi\ansicpg1252\cocoartf1504\cocoasubrtf830
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
{\*\expandedcolortbl;;}
\paperw11900\paperh16840\margl1440\margr1440\vieww10800\viewh8400\viewkind0
\pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\pardirnatural\partightenfactor0
\f0\fs24 \cf0 myText}
我刚刚写了 myText
,RTF 添加了很多 if own 标签。