louis1001
louis1001
TSDThe Swift Den
Created by louis1001 on 9/27/2023 in #swift-development
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)
}
5 replies