Xcode HTML в строку возвращает ноль? - SWIFT 3

-2
public func setHtmlBody(_ body: String, bounds: CGRect = UIScreen.main.bounds) -> String {

    let font = "-apple-system"
    let fontSize = 19
    let fontColor = "#000000"
    let lineHeight = 25
    let imageWidth = bounds.width - 40
    let margin = 20
    let codeStyle = "pre[class*=\"language-\"]{background:white;border-radius:14px;color:black;display:block;font-size:16px;font-weight:500;padding:20px;overflow-x:auto;white-space:pre-wrap;line-height:130%;}pre.language-coffeescript .token.comment{color:#6a7576}pre.language-coffeescript .token.string{color:#8ADC64}pre.language-coffeescript .token.number,pre.language-coffeescript .token.operator{color:#a580f8}pre.language-coffeescript .token.keyword,pre.language-coffeescript .token.class-name,pre.language-coffeescript .token.function{color:#8df}pre.language-swift .token.comment{color:#690}pre.language-swift .token.string{color:#ee433f}pre.language-swift .token.keyword{color:#C945A7}pre.language-swift .token.number,pre.language-swift .token.function,pre.language-swift .token.builtin,pre.language-swift .token.class-name{color:#5C2699}"

    let htmlString = "<style>p, li { font-family:\"\(font)\"; color: \(fontColor); font-size:\(fontSize)px; line-height:\(lineHeight)px } p { margin: \(margin)px 0; } img { max-width: \(imageWidth)px; } #p { font-weight: bold; font-size: 24px; line-height: 130%; margin: 50px 20px; } ul li, ol li { margin: 20px 0; font-weight: bold; } \(codeStyle)</style>\(body)"

    return htmlString
}

Я использую быстрый 3. Почему функция возвращает меня в Nil?

Usign:

bodyAttributedString = setHtmlBody(sectionBody).htmlToAttributedString
  • 0
    ваша функция возвращает String , а не String? , Так что не может быть ноль
  • 0
    bodyAttributedString = setHtmlBody (sectionBody) .htmlToAttributedString, в этом случае нет
Показать ещё 1 комментарий
Теги:
string
xcode
swift3

1 ответ

0

Это будет проблема с функцией htmlToAttributedString все остальные работают нормально. Я попробовал это с помощью аналогичного метода, который отлично работал со мной

Пожалуйста, проверьте...

Расширения для получения html в качестве строки Attributed

extension Data {
    var htmlToAttributedString: NSAttributedString? {
        do {
            return try NSAttributedString(data: self, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil)
        } catch {
            print("error:", error)
            return  nil
        }
    }
    var html2String: String {
        return htmlToAttributedString?.string ?? ""
    }
}

extension String {
    var htmlToAttributedString: NSAttributedString? {
        return Data(utf8).htmlToAttributedString
    }
    var html2String: String {
        return htmlToAttributedString?.string ?? ""
    }
}

Функции построения строки HTML

public func setHtmlBody(_ body: String, bounds: CGRect = UIScreen.main.bounds) -> String {

    let font = "-apple-system"
    let fontSize = 19
    let fontColor = "#000000"
    let lineHeight = 25
    let imageWidth = bounds.width - 40
    let margin = 20
    let codeStyle = "pre[class*=\"language-\"]{background:white;border-radius:14px;color:black;display:block;font-size:16px;font-weight:500;padding:20px;overflow-x:auto;white-space:pre-wrap;line-height:130%;}pre.language-coffeescript .token.comment{color:#6a7576}pre.language-coffeescript .token.string{color:#8ADC64}pre.language-coffeescript .token.number,pre.language-coffeescript .token.operator{color:#a580f8}pre.language-coffeescript .token.keyword,pre.language-coffeescript .token.class-name,pre.language-coffeescript .token.function{color:#8df}pre.language-swift .token.comment{color:#690}pre.language-swift .token.string{color:#ee433f}pre.language-swift .token.keyword{color:#C945A7}pre.language-swift .token.number,pre.language-swift .token.function,pre.language-swift .token.builtin,pre.language-swift .token.class-name{color:#5C2699}"

    let htmlString = "<style>p, li { font-family:\"\(font)\"; color: \(fontColor); font-size:\(fontSize)px; line-height:\(lineHeight)px } p { margin: \(margin)px 0; } img { max-width: \(imageWidth)px; } #p { font-weight: bold; font-size: 24px; line-height: 130%; margin: 50px 20px; } ul li, ol li { margin: 20px 0; font-weight: bold; } \(codeStyle)</style>\(body)"
    return htmlString
}

презентация

override func viewDidLoad() {
    super.viewDidLoad()

    let body = "<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>"

    let htmlString = setHtmlBody(body)


    if let attributedString = htmlString.htmlToAttributedString {
        self.labelHTML.attributedText = attributedString
    }
 }

Выход

Изображение 174551

Ещё вопросы

Сообщество Overcoder
Наверх
Меню