Подтвердить что ты не робот

Как добавить CSS html в NSAttributedString?

Я пытаюсь загрузить html файлы в nsatrributed строку, чтобы отобразить ее в UITextView..., Итак, как применять css для контента texview.,,

4b9b3361

Ответ 1

С iOS 7 вы можете использовать NSAttributedString с синтаксисом HTML:

NSURL *htmlString = [[NSBundle mainBundle]  URLForResource: @"string"     withExtension:@"html"];
NSAttributedString *stringWithHTMLAttributes = [[NSAttributedString alloc] initWithFileURL:htmlString
                                                                                       options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType}
                                                                            documentAttributes:nil
                                                                                         error:nil];
textView.attributedText = stringWithHTMLAttributes; // attributedText field!

Вам нужно добавить string.html к проекту, а содержимое html может быть таким:

<html>
  <head>
    <style type="text/css">
      body {
        font-size: 15px;
        font-family: Avenir, Arial, sans-serif;
        color: red;
      }
    </style>
  </head>
  <body>
    <p>This is the text</p>
  </body>
</html> 

Из того, что я могу проверить, вы можете изменить: цвет, шрифт-семейство, размер шрифта, размер шрифта, выравнивание по тексту и использовать <b>, <strong>, <i>, <u>, <sup> и <sub>.