Reputation: 945
I'm reading a rtf file but output result shows me simple string containing tags, It doesn't appear as NSAttributedString
as it should
here is my code
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)")
}
}
After reading the file its shows attribute string as :
< 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> .
I've inserted spaces in tag to clarify the issue, without spaces StackOverflow applies tag to text, what am I missing here ?
Upvotes: 0
Views: 549
Reputation: 26026
After RTF to NSAttributedString
you got (you can use code tag of SO to avoid the HTML tags to be interpreted):
<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>
So now, you have a HTML AttributedString.
And like RTF, there is a way to get NSAttributedString
from HTML String.
So let's get first the HTML String: let htmlString = attributedStringWithRtf.string
.
For the rest, the method is almost the same as the one with used, with changes: NSAttributedString.DocumentType.html
instead of NSAttributedString.DocumentType.rtf
(seems obvious), and there is an init with data
instead of path, so you just have to use let htmlData = htmlString.data(using:.utf8)
So the code:
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)
Note: I am not using if let
, try
/catch
on purpose because you already use them correctly, and I'm more interested in the logic behind your issue and how to get away with it.
Now, after discussion in comment:
You save HTML Text (ie with HTML tags) into a RTF file.
Clearly, that's overkill, because as you can see in the solution I gave you do twice the conversion to get the final NSAttributedString
.
So, what I'd do is either use entirely RTF File, and set the RTF tags equivalent for <p>
, <em>
, <strong>
, etc instead of the HTML ones.
Save you HTML String (with HTML tags) in a simple .txt
.
For instance, I saved a RTF File using TextEdit.app, and the real content is:
{\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}
And I just wrote myText
, RTF adding a lot of if own tags.
Upvotes: 2