Skip to content

Instantly share code, notes, and snippets.

Last active June 11, 2017 18:42
What would you like to do?
Use custom fonts in the editor
__author__ = "Lukas Kollmer<>"
__copyright__ = "Copyright (c) 2016 Lukas Kollmer<>"
from objc_util import *
from ctypes import *
CTFontManagerRegisterFontsForURL = c.CTFontManagerRegisterFontsForURL
CTFontManagerRegisterFontsForURL.argtypes = [c_void_p, c_int, c_void_p]
CTFontManagerRegisterFontsForURL.restype = c_bool
CFURLCreateWithString = c.CFURLCreateWithString
CFURLCreateWithString.argtypes = [c_void_p, c_void_p, c_void_p]
CFURLCreateWithString.restype = c_void_p
def load_custom_font(file):
font_url = CFURLCreateWithString(None, ns(file), None)
error = c_void_p(None)
# This returns false, but still succeds
CTFontManagerRegisterFontsForURL(ObjCInstance(font_url), 0, byref(error))
def set_editor_font(name, size=15):
NOTE: This still requires a restart
defaults = ObjCClass("NSUserDefaults").standardUserDefaults()
defaults.setObject_forKey_(name, "EditorFontName")
defaults.setObject_forKey_(size, "EditorFontSize")
def reset_custom_font():
defaults = ObjCClass("NSUserDefaults").standardUserDefaults()
defaults.setObject_forKey_("Menlo-Regular", "EditorFontName")
defaults.setObject_forKey_(15, "EditorFontSize")
if __name__ == "__main__":
import os
font_path = "file://" + os.path.expanduser("~/Documents/Fonts/SFMono/SFMono-Regular.otf")
font_name = "Menlo-Regular"
set_editor_font(font_name, 15)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment