从UIFont或CGFont获取元信息-iOS,Swift

时间:2018-11-18 10:18:52

标签: ios swift uifont

我想从字体中获取元信息。例如:支持的语言,版本,许可证等。我该怎么做?它在Mac字体应用程序上正常工作。我需要在iOS上实现此功能。 enter image description here

1 个答案:

答案 0 :(得分:0)

您可以使用CTFontCopyName获得很多信息。例如,您可以这样打印字体的版权和许可:

print(CTFontCopyName(font, kCTFontCopyrightNameKey))
print(CTFontCopyName(font, kCTFontLicenseNameKey))

键常量的格式为kCTFont***NameKey,并在CTFont.h中声明。

您可以使用CTFontCopySupportedLanguages获得受支持的语言,但是会获得BCP-47语言标识符的列表:

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

您可以使用Locale将它们转换为人类可读的语言名称。您可能要使用由用户选择的系统区域设置:

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

我不知道如何获得字体支持的脚本。