Преобразовать NSAttributedString в строку HTML - PullRequest
0 голосов
/ 11 сентября 2018

Я хочу только часть тела из строки HTML.

Ниже кода указана полная строка HTLM:

<!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>

Я хочу только часть ниже без 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>

Ответы [ 3 ]

0 голосов
/ 17 декабря 2018

На Mac, если вы по-прежнему хотите иметь стили, но хотите встроить их в свои теги, вы можете попросить NSAttributedString исключить теги стиля, например:

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

Таким образом, вы будете иметь все стили, встроенные в ваши теги.

К сожалению, это не доступно на iOS.

0 голосов
/ 14 марта 2019

Я использовал webView вместо textView для отображения атрибутивной строки.

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

Этот метод вернет вам HTML-строку без CSS.

0 голосов
/ 11 сентября 2018
 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);
 }
...