我无法更改html标签文字的颜色和字体大小,任何人都可以帮我解决这个问题吗?
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
答案 0 :(得分:0)
您应该使用 NSMutableAttributedString 来满足您的要求。试试这个
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
}