J'ai un contenu HTML que je veux rendre dans mon UILabel natif, mais en plus de cela, je dois ajouter une police personnalisée à cet UILabel. J'utilise NSAttributedString pour le faire et en utilisant le code suivant pour rendre le contenu HTML dans mon UILabel.Ajout d'attributs de police au rendu HTML UILabel swift
func htmlAttributedString() -> NSAttributedString? {
guard let data = self.data(using: .utf16, allowLossyConversion: false) else {
return nil
}
guard let html = try? NSMutableAttributedString(
data: data,
options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
documentAttributes: nil) else { return nil }
let attributes = [
NSForegroundColorAttributeName: UIColor.lightGray,
NSFontAttributeName : UIFont.systemFont(ofSize: 12)
]
html.setAttributes(attributes, range: NSRange(0..<html.length))
return html
}
var str = "Here is the <bold>string</bold> that i want to be bold.
messageLabel.attributedText = str.htmlAttributedString()
Le problème ici est que lorsque j'applique des attributs de police sur la chaîne HTML rendue. Ensuite, tout le formatage HTML comme gras est perdu.
Ainsi est-il possible que je peux appliquer une police sélectionnée sur HTML chaîne attribué rendu
https://stackoverflow.com/a/44435915/5362916 vérifier celui- –
Utilisez ce lien: https://stackoverflow.com/questions/28496093/making-text-bold-using-attributed-string -in-swift – ashwini
@UmaMadhavi votre lien a fonctionné pour moi. merci – Madu