I have a complex long XHTML file, which contains CSS. Searching on google and on this site, I’ve found some libraries that can be useful on XHTML parsing:
- NSXMLParser
- TBXML
- And some others
However, I’m wondering if there is any library for iPhone that can convert a xhtml + css document to a NSAttributedString (only the text, of course).
I have been thinking on that problem, and I have had some ideas, but I think it won’t be very efficient. My main idea is formed by this steps:
- Detect on the XTHML file all tags with an
idorclassattribute and get the range of the string where they have effect (I cannot achieve this). -
Save all the CSS attributes on a
NSDictionary, with moreNSDictionaryobjects inside. Something like this:mainDict { object: dictionary { object: @"#00ff00" key: @"color" object: @"1em" key: @"font-size" } key: @"a id" object: anotherDictionary { ... } key: @"another id" } -
Convert these CSS attributes dictionary on the
NSAttributedStringattributes dictionary.
I know that this is complex, and I don’t need you to provide the code (of course, if you provide it, it would be great), I only want the link to a library or, if it doesn’t exist, some advice for create a parser myself.
Of course, if you need some more information, ask by comments.
Thanks you!!
It depends on your needs if this will do what you want, but DTCoreText has an HTML -> NSAttributedString converter. It’s very specific for what DTCoreText wants to / needs to do, but it might at least point you in the right direction.