问题
I want to get meta info from fonts. For example: supported languages, version, license etc. How can I do this? It works fine on Mac Fonts app. I need to implement this on iOS.
回答1:
You can get much of that information using CTFontCopyName
. For example, you can print the font's copyright and license like this:
print(CTFontCopyName(font, kCTFontCopyrightNameKey))
print(CTFontCopyName(font, kCTFontLicenseNameKey))
The key constants are of the format kCTFont***NameKey
and are declared in CTFont.h.
You can get the supported languages using CTFontCopySupportedLanguages
, but you get a list of BCP-47 language identifiers:
import CoreText
import Foundation
let font = CTFontCreateWithName("Helvetica" as CFString, 12, nil)
let languageIds = CTFontCopySupportedLanguages(font) as! [String]
print(languageIds.joined(separator: ", "))
// Output:
af, agq, ak, asa, ast, az, bas, be, bem, bez, bg, bm, br, bs, ca, ce, cgg, cs, cy, da, dav, de, dje, dsb, dyo, ebu, el, en, eo, es, et, eu, ewo, fi, fil, fo, fr, fur, fy, ga, gd, gl, gsw, guz, gv, haw, hr, hsb, hu, id, ig, is, it, jmc, ka, kam, kde, kea, khq, ki, kk, kl, kln, ksb, ksf, ksh, kw, lag, lb, lg, lkt, ln, lt, lu, luo, luy, lv, mas, mer, mfe, mg, mgh, mgo, mk, mn, ms, mt, naq, nb, nd, nl, nmg, nn, nnh, nyn, om, os, pl, pt, qu, rm, rn, ro, rof, ru, rw, rwk, saq, sbp, se, seh, ses, sg, sk, sl, smn, sn, so, sq, sr, sv, sw, teo, tg, tk, to, tr, twq, uk, uz, vi, vun, wae, xog, yav, yo, zu
You can convert them to human-readable language names by using a Locale
. You probably want to use the system locale, chosen by the user:
let myLocale = Locale.autoupdatingCurrent
let languageNames = languageIds.compactMap({ myLocale.localizedString(forLanguageCode: $0) })
print(languageNames.joined(separator: ", "))
// Output:
Afrikaans, Aghem, Akan, Asu, Asturian, Azerbaijani, Basaa, Belarusian, Bemba, Bena, Bulgarian, Bambara, Breton, Bosnian, Catalan, Chechen, Chiga, Czech, Welsh, Danish, Taita, German, Zarma, Lower Sorbian, Jola-Fonyi, Embu, Greek, English, Esperanto, Spanish, Estonian, Basque, Ewondo, Finnish, Filipino, Faroese, French, Friulian, Western Frisian, Irish, Scottish Gaelic, Galician, Swiss German, Gusii, Manx, Hawaiian, Croatian, Upper Sorbian, Hungarian, Indonesian, Igbo, Icelandic, Italian, Machame, Georgian, Kamba, Makonde, Kabuverdianu, Koyra Chiini, Kikuyu, Kazakh, Kalaallisut, Kalenjin, Shambala, Bafia, Colognian, Cornish, Langi, Luxembourgish, Ganda, Lakota, Lingala, Lithuanian, Luba-Katanga, Luo, Luyia, Latvian, Masai, Meru, Morisyen, Malagasy, Makhuwa-Meetto, Metaʼ, Macedonian, Mongolian, Malay, Maltese, Nama, Norwegian Bokmål, North Ndebele, Dutch, Kwasio, Norwegian Nynorsk, Ngiemboon, Nyankole, Oromo, Ossetic, Polish, Portuguese, Quechua, Romansh, Rundi, Romanian, Rombo, Russian, Kinyarwanda, Rwa, Samburu, Sangu, Northern Sami, Sena, Koyraboro Senni, Sango, Slovak, Slovenian, Inari Sami, Shona, Somali, Albanian, Serbian, Swedish, Swahili, Teso, Tajik, Turkmen, Tongan, Turkish, Tasawaq, Ukrainian, Uzbek, Vietnamese, Vunjo, Walser, Soga, Yangben, Yoruba, Zulu
I don't know how to get the scripts supported by a font.
来源:https://stackoverflow.com/questions/53359789/get-meta-info-from-uifont-or-cgfont-ios-swift