0

I have a label showing HTML through this NSAttributedString function. The problem is that I want to add a custom font to it, but it does not add the font.

What have I done wrong?

Here is my code:

extension String { func convertHtml() -> NSAttributedString { let titleAttributes = [NSAttributedStringKey.font: UIFont.preferredFont(forTextStyle: UIFontTextStyle.headline), NSAttributedStringKey.foregroundColor: UIColor.black] guard let data = data(using: .utf8) else { return NSAttributedString() } do{ return try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil) }catch{ return NSAttributedString.init(string: self, attributes: titleAttributes) } } } 
2
  • 1
    You make no use of titleAttributes if the attributed string is created as HTML from the string. Commented Jan 31, 2018 at 0:27
  • @rmaddy Aaaah that's right. But how do I add it to the NSAttributedString inside the do? Commented Jan 31, 2018 at 0:28

1 Answer 1

2

If you wish to replace all font settings from the resulting HTML, you need to apply your titleAttributes after creating the attributed string from the HTML string.

extension String { func convertHtml() -> NSAttributedString { let titleAttributes = [NSAttributedStringKey.font: UIFont.preferredFont(forTextStyle: UIFontTextStyle.headline), NSAttributedStringKey.foregroundColor: UIColor.black] guard let data = data(using: .utf8) else { return NSAttributedString() } do{ let res = try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil) let mutable = res.mutableCopy() as! NSMutableAttributedString mutable.addAttributes(titleAttributes, range: NSRange(location: 0, length: res.length)) return mutable }catch{ return NSAttributedString.init(string: self, attributes: titleAttributes) } } } 
Sign up to request clarification or add additional context in comments.

6 Comments

Get an error here: let mutable = [res mutableCopy] saying it needs to separate res and mutableCopy and that it cannot find mutableCopy?
If I change it to [res.mutableCopy()] it says that [Any] cannot be a NSMutableAttributedString
Worked without the []
Oops. Old Objective-C habit. Fixed answer.
I know it was already like this but guard let data = data(using: .utf8) else { is pointless. The else part will never be executed considering that Swift strings are utf8. You can simply force unwrap or you can use Data initializer and pass the string utf8 property. Btw why don't you use NSMutableAttributedString initializer ? NSMutableAttributedString(data: Data(utf8), options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.