将 NSAttributedString 转换为 HTML 字符串
Convert NSAttributedString to HTML String
我只想要 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>
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);
}
在 mac,如果您仍然想要样式,但希望将它们嵌入到您的标签中,您可以要求 NSAttributedString 排除样式标签,如下所示:
NSDictionary *documentAttributes = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSExcludedElementsDocumentAttribute: @[@"style"]
};
NSData *htmlData = [attributedString dataFromRange:NSMakeRange(0, attributedString.length) documentAttributes:documentAttributes error:NULL];
这样,您的标签中就会嵌入所有样式。
遗憾的是,它在 iOS 上不可用。
我使用 webView 而不是 textView 来显示属性字符串。
NSString *strState = [webView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML"];
此方法将 return 给你 HTML 没有 CSS 的字符串。
我只想要 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>
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);
}
在 mac,如果您仍然想要样式,但希望将它们嵌入到您的标签中,您可以要求 NSAttributedString 排除样式标签,如下所示:
NSDictionary *documentAttributes = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSExcludedElementsDocumentAttribute: @[@"style"]
};
NSData *htmlData = [attributedString dataFromRange:NSMakeRange(0, attributedString.length) documentAttributes:documentAttributes error:NULL];
这样,您的标签中就会嵌入所有样式。
遗憾的是,它在 iOS 上不可用。
我使用 webView 而不是 textView 来显示属性字符串。
NSString *strState = [webView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML"];
此方法将 return 给你 HTML 没有 CSS 的字符串。