iOS字体安装和使用

前言

自7.0版本起iOS系统就支持字体文件的安装,以配置描述文件的形式储存在系统中。安装完成后即可通过各种App使用该字体。

代码

# coding: utf-8
import re
import hashlib
import sys
from base64 import b64encode
from random import choice

items = ['8', '9', 'a', 'b']
names = sys.argv

assert len(names) > 1

def replace(m):
    return "-".join([m.group(1), m.group(2), '5' + m.group(3), choice(items) + m.group(5), m.group(5)])

for name in names[1:]:

    with open(name, "rb") as f:
        font_data = f.read()

    sha1_font_data = hashlib.sha1(font_data).hexdigest()

    UUID = replace(re.search("([a-f0-9]{8})([a-f0-9]{4})([a-f0-9]{3})([a-f0-9]{3})([a-f0-9]{12})", sha1_font_data))

    encoded_font_data = b64encode(font_data)

    xml = f"""<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
        <key>PayloadUUID</key>
        <string>{ UUID }</string>
        <key>PayloadVersion</key>
        <integer>1</integer>
        <key>PayloadIdentifier</key>
        <string>geniuscqy.font-{ UUID }</string>
        <key>PayloadDisplayName</key>
        <string>{ name }</string>
        <key>PayloadOrganization</key>
        <string>Font Profile</string>
        <key>PayloadDescription</key>
        <string>Make the font available to iOS applications.</string>
        <key>PayloadContent</key>
        <array>
            <dict>
                <key>PayloadVersion</key>
                <integer>1</integer>
                <key>PayloadUUID</key>
                <string>{ UUID }</string>
                <key>Font</key>
                <data>{ encoded_font_data.decode() }</data>
                <key>PayloadType</key>
                <string>com.apple.font</string>
                <key>PayloadIdentifier</key>
                <string>LoggingPayload</string>
            </dict>
        </array>
        <key>PayloadType</key>
        <string>Configuration</string>
        <key>PayloadRemovalDisallowed</key>
        <false/>
    </dict>
    </plist>"""

    with open(name+".mobileconfig", "w", encoding="utf-8") as f:
        f.write(xml)

Packaging fonts in configuration profiles for installing on iOS | Gist

使用

先下载代码重命名为packagingfonts.py,和需要安装的字体文件(.ttf或.otf格式)放在同个目录下

贡献几个美渣渣的字体来源

CMD中运行代码

python packagingfonts.py SentyMarukoPro.ttf NotoSans-Regular.ttf

目录下就会多出两个配置描述文件

NotoSans-Regular.ttf.mobileconfig
SentyMarukoPro.ttf.mobileconfig

可这个描述文件需要用Safari打开才行。
把计算机和iOS设备连接到同个Wifi上,在CMD中输入以下命令

>>> python -m http.server -b 192.168.137.1
Serving HTTP on 192.168.137.1 port 8000 (http://192.168.137.1:8000/) ...

然后用Safari访问该地址,选择相应的设备描述文件安装字体即可。

Pages

参考

原文链接 iOS字体安装和使用

推荐阅读更多精彩内容