0

This is my HTML sample string.

"<!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: 40.0px; font: 33.2px '.SF UI Text'; color: #000000; -webkit-text-stroke: #000000}span.s1 {font-family: '.SFUIText'; font-weight: normal; font-style: normal; font-size: 33.19pt; font-kerning: none}</style></head><body><p class=\"p1\"><span class=\"s1\">123</span></p></body></html>"

Here are two functions to converting from HTML to attribute string to HTML for dealing server size too.

Attribute string to HTML:

func attributeToHTML(_ attr: NSAttributedString) -> String {

    var resultHtmlText = ""
    do {

        let r = NSRange(location: 0, length: (attr.length))
        let att = [NSAttributedString.DocumentAttributeKey.documentType: NSAttributedString.DocumentType.html]
        let d = try attr.data(from: r, documentAttributes: att)

        if let h = String(data: d, encoding: .utf8) {
            resultHtmlText = h
        }
    }
    catch {
        print("utterly failed to convert to html!!! \n>\(String(describing: attr))<\n")
    }
    return resultHtmlText
}

HTML to Attributed string:

func htmlToAttribute(_ string: String) -> NSAttributedString {
    let htmlString = string
    var result = NSAttributedString()
    do {
        let options = [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html]
        let str = try NSAttributedString(data: htmlString.data(using: String.Encoding.unicode, allowLossyConversion: true)!, options: options, documentAttributes: nil)
        result = attributedString
    } catch {
        print(error)
    }

    return result
}

When I convert HTML string to attributed string it increases the font size, and working perfect with attributed string to HTML conversion.

By enumerating objects I can edit font size too, but its dynamic content any type of HTML string get in response. So this is not solutions.

Please suggest the way how to deal with HTML content in UITextview for editing and storing in server.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Solid Soft
  • 1,872
  • 2
  • 25
  • 55
  • https://stackoverflow.com/questions/20992950/ios7-font-size-change-when-create-nsattributedstring-from-html https://stackoverflow.com/questions/44590184/swift-font-size-increases-when-converting-html-string-to-attributed-string-and https://stackoverflow.com/questions/28441486/html-to-nsattributedstring-and-nsattributedstring-to-html ? – Larme Jul 27 '18 at 15:11
  • I already mentioned, By enumerating objects I can edit font size too, but its dynamic content any type of HTML string get in response. So this is not solutions. – Solid Soft Jul 30 '18 at 12:05
  • It's explained that there seems to be a bug: " I don´t know why he is convert px to pt". It seems to be on Cocoa(Touch), so there shouldn't be a lot you can do except modify them. – Larme Jul 30 '18 at 12:08

0 Answers0