I have some localization strings like this: "Register and get <b>all</b>\n<b>rewards cards</b> of our partners\n<b>in one</b> universal <b>card</b>"
So parts of it should be bold, and also there is a requirement for paragraph style.
This code works perfect for styling except bold parts:
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 1.0
paragraphStyle.alignment = .center
paragraphStyle.minimumLineHeight = 18.0
let attributedString = NSMutableAttributedString(string: descriptionString, attributes: [.font: UIFont.proximaNovaRegularWithSize(size: 15.0),
.paragraphStyle: paragraphStyle])
Also I have perfect solution to parse html tags:
func convertFromHTMLString(_ input: String?) -> NSMutableAttributedString? {
guard let input = input else { return nil }
guard let data = input.data(using: String.Encoding.unicode, allowLossyConversion: true) else { return nil }
return try? NSMutableAttributedString(data: data, options: [NSAttributedString.DocumentReadingOptionKey.documentType : NSAttributedString.DocumentType.html], documentAttributes: nil)
}
But it's not working combined. So my question - how to parse html tags and add attributes to it? And the solution should be totally dynamic, to be used for different localization.
Note: I can't use markdown text as app should be available for iOS lower than 15.0
After used proposed solution
let attr = try? NSMutableAttributedString(
data: data ?? Data(),
options: [
.documentType: NSAttributedString.DocumentType.html,
.characterEncoding: String.Encoding.utf8.rawValue,
.defaultAttributes: [
NSAttributedString.Key.font: UIFont.proximaNovaRegularWithSize(size: 15.0),
NSAttributedString.Key.paragraphStyle: paragraphStyle
],
],
documentAttributes: nil
)
still no font and styling

