Skip to content Skip to sidebar Skip to footer

Html To Nsattributedstring And Nsattributedstring To Html

I want to convert a html string to NSAttributedString and then work on the string like (change colors, fontsizes, fontfamily, background- , foreground-color...) and then convert th

Solution 1:

I solved this by applying 0.75 ratio on each fond size in your string. Say if you have multiple fonts in your attributed string, when you looping though all of them, simple apply the ratio and then you are all set. Here is my code in swift 3.0:

yourAttrStr.beginEditing()
yourAttrStr.enumerateAttribute(NSFontAttributeName, in: NSMakeRange(0, yourAttrStr.length), options: .init(rawValue: 0)) { 
        (value, range, stop) iniflet font = value as?UIFont {
            let resizedFont = font.withSize(font.pointSize *0.75)
            yourAttrStr.addAttribute(NSFontAttributeName, value: resizedFont, range: range)
        }
}
yourAttrStr.endEditing()//yourAttrStr will be the same size as html string

This is the piece of code that is running in my application

extensionString {
    funchtmlAttributedString() -> NSAttributedString? {
        guardlet data =self.data(using: String.Encoding.utf16, allowLossyConversion: false) else { returnnil }
        guardlet html =try?NSMutableAttributedString(
            data: data,
            options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
            documentAttributes: nil) else { returnnil }
        html.beginEditing()
        html.enumerateAttribute(NSFontAttributeName, in: NSMakeRange(0, html.length), options: .init(rawValue: 0)) {
            (value, range, stop) iniflet font = value as?UIFont {
                let resizedFont = font.withSize(font.pointSize *0.75)
                html.addAttribute(NSFontAttributeName,
                                         value: resizedFont,
                                         range: range)
            }
        }
        html.endEditing()
        return html
    }
}

To use it:

let htmlStr: String="<font size=\"6\">font a</font><font size=\"16\">Font b</font>"let attriStr: NSAttributedString? = htmlStr.htmlAttributedString()

Solution 2:

In Swift 3 taking @fangming's solution, that worked for me:

funcnewAttrSize(blockQuote: NSAttributedString) -> NSAttributedString
{
    let yourAttrStr =NSMutableAttributedString(attributedString: blockQuote)
    yourAttrStr.enumerateAttribute(.font, in: NSMakeRange(0, yourAttrStr.length), options: .init(rawValue: 0)) {
        (value, range, stop) iniflet font = value as?UIFont {
            let resizedFont = font.withSize(font.pointSize *0.75)
            yourAttrStr.addAttribute(.font, value: resizedFont, range: range)
        }
    }

    return yourAttrStr
}

Solution 3:

It's probably rounding errors in the round-trip. Try using integer point sizes (with pt instead of px)

Ok, looking at your console output, it's translating your px to pt, so maybe you can hack it by taking the HTML that comes from the conversion and changing pt back to px.

Post a Comment for "Html To Nsattributedstring And Nsattributedstring To Html"