1

Thanks in advance! I'm wondering if it's possible to convert HTML code stored in a NSString to a NSData to parse later. I'm reading from a BBDD the HTML code and saving it into a NSString.

NSString *htmlString =@"<html><body><p>introduccion</p><p>introducción</p></body></html>"; 

I want to use:

NSData *nsData = [[NSData alloc] initWithContentsOfURL:url];

But instead of initWithContentsOfURL i have to use the htmlString because I have the code stored in a BBDD and I am accessing it and storing the code to a NSString

Thanks!

2 Answers 2

2

Why dont you use standard methods to convert NSString to NSData like this:

NSData* data = [htmlString dataUsingEncoding:NSUTF8StringEncoding]; 
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, it doesn't works for me I'm trying with this: NSString htmlString = @"... SOME HTML CODE...."; NSData nsData = [htmlString dataUsingEncoding:NSUTF8StringEncoding];
0

I have some html formatted text in my "subject.details", I add some more html formatting on the front and back. Giving me a "detailsStringForDisplay" NSString.

I convert this string to an NSData. Set up a dictionary, which only contains one element to say that the document type is HTML.

Then finally set an attributedText using the data and the dictionary.

 NSString *fontDetailsHTML = @"<div style=\"font-size:17px; text-family:Georgia; \">"; NSString *detailsString = subject.details; NSString *endFontDetailsHTML = @"</div>"; NSString *detailsStringForDisplay = [NSString stringWithFormat:@"%@%@%@",fontDetailsHTML,detailsString,endFontDetailsHTML]; NSData *labelTextData = [detailsStringForDisplay dataUsingEncoding:NSUnicodeStringEncoding allowLossyConversion:true]; NSDictionary *attributesForText = [[NSDictionary alloc]initWithObjectsAndKeys:NSHTMLTextDocumentType,NSDocumentTypeDocumentAttribute , nil]; self.detailDescriptionLabel.attributedText = [[NSAttributedString alloc]initWithData:labelTextData options:attributesForText documentAttributes:nil error:nil]; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.