I will like to provide the list of fonts that support the localised language. I could not find anything in UIFont and Font; but found something in CTFont and implemented the code below.
HOWEVER, I keep getting warnings like "CoreText note: Client requested name ".SFUI-BoldG4", it will get TimesNewRomanPSMT rather than the intended font. All system UI font access should be through proper APIs such as CTFontCreateUIFontForLanguage() or +[UIFont systemFontOfSize:]."
Is there a better alternative in Swift to retrieve the system font list of a specific language?
import CoreText
import Foundation
import SwiftUI
extension CTFont {
func supportedLanguages(_ fontName: String, _ size: CGFloat = DEFAULT_USER_FONT_SIZE) -> [String] {
let font = CTFontCreateWithName(fontName as CFString, size, nil)
let languageIds = CTFontCopySupportedLanguages(font) as! [String]
return languageIds
}
}
extension UIFont {
static func supportedFontNames(language: String = "", size: CGFloat = DEFAULT_USER_FONT_SIZE) -> [String] {
var fontNames: [String] = []
for familyName in UIFont.familyNames {
for fontName in UIFont.fontNames(forFamilyName: familyName) {
if !fontNames.contains(fontName), CTFont(fontName as CFString, size: size).supportedLanguages(fontName, size).contains(language) {
fontNames.append(fontName)
}
}
}
return fontNames
}
}
This can be more easily implemented using a CTFontCollection: