2017-10-11 4 views
0

Je ne peux pas modifier la couleur du texte de la balise html et la taille de la police. Quelqu'un peut-il m'aider à résoudre ce problème?Comment changer la taille et la taille des caractères des balises html dans swift 3?

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 

Répondre

0

Vous devez utiliser NSMutableAttributedString pour atteindre vos besoins. Essayez ceci

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 
}