1

I want Only Body Part from HTML String.

Below code is Full HTLM String:

<!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; line-height: 45.0px; font: 37.9px 'Times New Roman'; color: #000000; -webkit-text-stroke: #000000}
span.s1 {font-family: 'Times New Roman'; font-weight: normal; font-style: normal; font-size: 37.92pt; font-kerning: none}
span.s2 {font-family: 'TimesNewRomanPS-BoldMT'; font-weight: bold; font-style: normal; font-size: 37.92pt; font-kerning: none}
</style>
</head>
<body>
<p class="p1"><span class="s1">-1 Water damage and dry-rot observed on fascia boards around </span><span class="s2">the perimeter of the structur</span><span class="s1">e.</span></p>
</body>
</html>

I want Only Below Part Without CSS.

<body>
<p class="p1"><span class="s1">-1 Water damage and dry-rot observed on fascia boards around </span><span class="s2">the perimeter of the structur</span><span class="s1">e.</span></p>
</body>
1
  • If you don't import the class definition <style ... span.s1 {...}</style> you might get strange result, no? Commented Sep 11, 2018 at 14:19

3 Answers 3

1

I used webView instead of textView to display attributed string.

NSString *strState = [webView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML"];

This method will return to you HTML string without CSS.

Sign up to request clarification or add additional context in comments.

Comments

0
 NSString * string;
 NSString * pattern;

 string = html// [NSString stringWithContentsOfURL:[[NSBundle mainBundle]  URLForResource:@"File" withExtension:nil] encoding:NSASCIIStringEncoding error:nil];
 pattern = @"<body>[ \\w\\d\\n<>=\\\"-/]*</body>";

 NSRegularExpression *   regex =  [[NSRegularExpression alloc]initWithPattern:pattern options:(NSRegularExpressionAnchorsMatchLines) error:nil] ;
 NSTextCheckingResult * result = [regex firstMatchInString:string options:0 range:NSMakeRange(0, string.length)];
  if (result != nil){
     NSString * resultString  = [string substringWithRange: result.range];
     NSLog(resultString);
 }

Comments

0

On mac, if you still want to have styles, but want to have them embedded in your tags, you can ask NSAttributedString to exclude style tags like this :

NSDictionary *documentAttributes = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                                     NSExcludedElementsDocumentAttribute: @[@"style"]
                                     };
NSData *htmlData = [attributedString dataFromRange:NSMakeRange(0, attributedString.length) documentAttributes:documentAttributes error:NULL];

In this way, you will have all the styles embedded in your tags.

Unfortunately it's not available on iOS.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.