简体   繁体   中英

how to change html tags text font size and colour in swift 3?

I am unable to change the html tag text color and size of font can anyone help me how to resolve this ?

let cell = tableView.dequeueReusableCell(withIdentifier: "descriptioncell", for: indexPath) as! DescriptionTableViewCell
let attrStr = try! NSAttributedString(data: (descriptionAttribute?.data(using: String.Encoding.unicode, allowLossyConversion: true)!)!,
                                                  options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
                                                  documentAttributes: nil)
cell.descriptionTextView.attributedText = attrStr

You should use NSMutableAttributedString to achieve your requirement. Try this

func convertToInlineImageFormat(htmlStrr:String) -> NSMutableAttributedString {

    let htmlStringgg = htmlStrr as String
    let content = try! NSMutableAttributedString(
        data: htmlStringgg.data(using: String.Encoding.unicode, allowLossyConversion: true)!,
        options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
        documentAttributes: nil)
    //        content.addAttribute(NSForegroundColorAttributeName, value: UIColor.white, range: NSRange(location: 0, length: htmlStrr.length))

    // Resizing Inline images
    content.enumerateAttribute(NSAttachmentAttributeName, in: NSMakeRange(0, content.length), options: NSAttributedString.EnumerationOptions.init(rawValue: 0), using: { (value, range, stop) -> Void in
        if let attachement = value as? NSTextAttachment {
            let image = attachement.image(forBounds: attachement.bounds, textContainer: NSTextContainer(), characterIndex: range.location)
            let screenSize: CGRect = UIScreen.main.bounds
            if (image?.size.width)! > screenSize.width - 2 {
                let newImage = image?.resizeImage(scale: (screenSize.width - 2)/(image?.size.width)!)
                let newAttribut = NSTextAttachment()

                newAttribut.image = newImage
                content.addAttribute(NSAttachmentAttributeName, value: newAttribut, range: range)

            }
        }
        // ARTICLE DESCRIPTION FONT
        //            let replacementFont = Contants.descFont

        let fontSizeDescribtion = UserDefaults.standard.float(forKey: "FontSize")
        var fontSize :Float = 0.0
        if fontSizeDescribtion == 0{
            fontSize = 18
        }else{
            fontSize = Float(fontSizeDescribtion)
        }
        let fontDesc =  UIFont(name: Contants.abiramiFont, size: CGFloat(Float(fontSize)))

        content.addAttribute(NSFontAttributeName, value: fontDesc!, range: NSRange(location: 0, length: content.length))
        content.addAttribute(NSForegroundColorAttributeName, value: UIColor.black, range: NSRange(location: 0, length: content.length))

    }) // Content block

    return content
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM