feat: retrieve system fonts on iOS and Android and show font weight variants, closes #949 and closes #557 (#976)

This commit is contained in:
Huang Xin 2025-04-26 23:37:04 +08:00 committed by GitHub
parent 9303ec8c5f
commit a424ae8b15
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
25 changed files with 254 additions and 84 deletions

View file

@ -1,11 +1,27 @@
import AuthenticationServices
import AVFoundation
import AuthenticationServices
import CoreText
import MediaPlayer
import SwiftRs
import Tauri
import UIKit
import WebKit
func getLocalizedDisplayName(familyName: String) -> String? {
let fontDescriptor = CTFontDescriptorCreateWithAttributes(
[
kCTFontFamilyNameAttribute: familyName
] as CFDictionary)
let font = CTFontCreateWithFontDescriptor(fontDescriptor, 0.0, nil)
var actualLanguage: Unmanaged<CFString>?
if let localizedName = CTFontCopyLocalizedName(font, kCTFontFamilyNameKey, &actualLanguage) {
return localizedName as String
}
return nil
}
class SafariAuthRequestArgs: Decodable {
let authUrl: String
}
@ -95,6 +111,25 @@ class NativeBridgePlugin: Plugin {
}
invoke.resolve(["success": true])
}
@objc public func get_sys_fonts_list(_ invoke: Invoke) throws {
var fontList: [String] = []
for family in UIFont.familyNames.sorted() {
if let localized = getLocalizedDisplayName(familyName: family) {
fontList.append(localized)
} else {
let fontNames = UIFont.fontNames(forFamilyName: family)
if fontNames.isEmpty {
fontList.append(family)
} else {
fontList.append(contentsOf: fontNames)
}
}
}
invoke.resolve(["fonts": fontList])
}
}
@_cdecl("init_plugin_native_bridge")