NSAttributedString from html is not loading resources

I'm trying to move from NSAttributedString.init(data:options:) to NSAttributedString.fromHTML(_:options), which is asynchronous. It's working fine, except that the CSS inside the html doesn't load my custom fonts. The previous version loaded then without any problem. I can share the code, but it's very app specific. This is the CSS I want working correctly.
@font-face {
font-family: 'Circular Std';
src: local('CircularStd-Medium'), url('CircularStd-Medium.ttf') format('truetype');
font-weight: 500;
font-style: normal;
}
@font-face {
font-family: 'Circular Std';
src: local('CircularStd-Medium'), url('CircularStd-Medium.ttf') format('truetype');
font-weight: 500;
font-style: normal;
}
I just want to know if there's any additional config I need to get the resources loaded. This code works fine, but hangs if too there are too many calls at the same time:
if let attr = htmlProsesado.htmlToAttributedString {
let mutAttr = NSMutableAttributedString(attributedString: attr)

mainAsync {
self.questionLabel?.attributedText = mutAttr
}
}
if let attr = htmlProsesado.htmlToAttributedString {
let mutAttr = NSMutableAttributedString(attributedString: attr)

mainAsync {
self.questionLabel?.attributedText = mutAttr
}
}
And this code is the one that works, but doesn't load my fonts:
Task {
let (str, attrs) = try await NSAttributedString.fromHTML(html, options: [.documentType: NSAttributedString.DocumentType.html, .readAccessURL: Bundle.main.resourceURL!])
print(str)
}
Task {
let (str, attrs) = try await NSAttributedString.fromHTML(html, options: [.documentType: NSAttributedString.DocumentType.html, .readAccessURL: Bundle.main.resourceURL!])
print(str)
}
2 Replies
louis1001
louis100113mo ago
According the the docs, I can only set the DocumentReadingOptionKey for baseURL on macOS, not on iOS. My guess is that is why it fails. @Community Coach Does anyone know about this html conversion? @Coach I understand if it's too early to ping, but I need some help
aelfled
aelfled13mo ago
I'm not sure that using css to inject fonts not installed on the device is an intended use-case of NSAttributedString https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/AttributedStrings/Tasks/CreatingAttributedStrings.html#//apple_ref/doc/uid/20000714-BBCCGGCC The methods translate HTML as well as possible into structures of the Cocoa text system, but the Application Kit does not provide complete, true rendering of arbitrary HTML
Creating Attributed Strings in Cocoa
Explains how to use attributed strings, which manage attributes of character strings or individual characters.
Want results from more Discord servers?
Add your server