2014-01-27 4 views
3

Я разрабатываю приложение в iOS, и моим требованием является скрытая строка HTML для строки NSAttributed и строка NSAttributed обратно в HTML. Первая часть, которую я смог достичь. Но NSAttributed String для HTML не происходит. Он возвращает строку в HTML, но не по желанию. Пожалуйста, смотрите код и выход ниже:Строка NSAttribute для HTML

HTML для NSAttributed Строка

NSString *htmlString = @"<div>Test, </div><div>&nbsp;</div><div>Test Test Test</div><div>Test Test Test </div><div>&nbsp;</div><div>Format text:</div><ul style="margin:0;padding-left:36pt;"><li><b>bold text</b></li><li><i>italic text</i></li><li><font color="red"><i>red text</i></font></li></ul><div>&nbsp;</div><div><i>The meeting body</i><i> </i><i>attachments</i></div>"; 

    NSAttributedString *normal = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]} documentAttributes:nil error:nil]; 




textView = [[UITextView alloc]initWithFrame:CGRectMake(10, 20, 500, 500)]; 
    textView.attributedText = normal; 
    [self.view addSubview:textView]; 

Приведенный выше код работает по желанию ....

NSAttributed Строка HTML

NSAttributedString *s = textView.attributedText; 
    NSDictionary *documentAttributes = [NSDictionary dictionaryWithObjectsAndKeys:NSHTMLTextDocumentType, NSDocumentTypeDocumentAttribute, nil]; 
    NSData *htmlData = [s dataFromRange:NSMakeRange(0, s.length) documentAttributes:documentAttributes error:NULL]; 
    NSString *htmlString = [[NSString alloc] initWithData:htmlData encoding:NSUTF8StringEncoding]; 

Этот код не работает .. и я не получаю желания d выход .. выхода я получаю:

<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; font: 12.0px 'Times New Roman'; color: #000000; -webkit-text-stroke: #000000} 

span.s1 {font-family: 'Times New Roman'; font-weight: normal; font-style: normal; font-size: 12.00pt; font-kerning: none} 
</style> 
</head> 
<body> 
<p class="p1"><span class="s1">Test<span class="Apple-converted-space"> </span></span></p> 
<p class="p1"><span class="s1"> </span></p> 
<p class="p1"><span class="s1">Test Test<span class="Apple-converted-space"> </span></span></p> 
<p class="p1"><span class="s1"> </span></p> 
<p class="p1"><span class="s1"> </span></p> 
<p class="p1"><span class="s1"> </span></p> 
<p class="p1"><span class="s1"> </span></p> 
</body> 
</html> 

ответ

0

После отсылая некоторые ссылки я Найти это решение и я использовал мое приложение будет отлично работать. Загрузите свой HTML в фиктивный веб-просмотр и получите html body. вы получаете свои исходные html-данные.

NSAttributedString *s = textView.attributedText; 
    NSDictionary *documentAttributes = [NSDictionary dictionaryWithObjectsAndKeys:NSHTMLTextDocumentType, NSDocumentTypeDocumentAttribute, nil]; 
    NSData *htmlData = [s dataFromRange:NSMakeRange(0, s.length) documentAttributes:documentAttributes error:NULL]; 
    NSString *htmlString = [[NSString alloc] initWithData:htmlData encoding:NSUTF8StringEncoding]; 
    [webView loadHTMLString:htmlString baseURL:nil]; 
    NSString *BodyHTML = [webEditor stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML"]; 

PS: Если вы используете NSFontAttributeName в NSAttributedString Удалить NSFontAttributeName из NSAttributedString. ***** Спасибо, я что-то узнал из вашего вопроса. *****

+0

Что такое webEditor в вашей последней строке кода? –

+0

Это объект WebView. –

Смежные вопросы