PyObjC CoreText Framework Bindings

12.1 · active · verified Sun Apr 12

This library provides Python wrappers for the CoreText framework on macOS, enabling Python applications to interact with macOS's advanced text layout and font rendering capabilities. It is part of the larger PyObjC project, which bridges Python and Objective-C/Cocoa. The current version is 12.1, and releases typically align with new macOS SDKs and Python versions.

Warnings

Install

Imports

Quickstart

This example demonstrates how to import and use basic CoreText functions to create and inspect a font object. It checks for the framework's availability and then uses `CTFontDescriptorCreateWithNameAndSize` and `CTFontCreateWithFontDescriptor` to instantiate a font, finally printing its display name.

from CoreText import CTFontDescriptorCreateWithNameAndSize, CTFontCreateWithFontDescriptor, CTFontGetDisplayName
from Foundation import NSBundle

# Check if CoreText is available (generally true on macOS)
if NSBundle.bundleWithPath_('/System/Library/Frameworks/CoreText.framework'):
    print('CoreText framework is available.')

    # Create a font descriptor for Helvetica at 16 points
    font_descriptor = CTFontDescriptorCreateWithNameAndSize('Helvetica', 16.0)
    if font_descriptor:
        # Create a CTFontRef object from the descriptor
        font = CTFontCreateWithFontDescriptor(font_descriptor, 0.0) # 0.0 for default font matrix
        if font:
            font_name = CTFontGetDisplayName(font)
            print(f'Successfully created font: {font_name}')
            print(f'Font object type: {type(font)}')
        else:
            print('Failed to create font from descriptor.')
    else:
        print('Failed to create font descriptor.')
else:
    print('CoreText framework not found on this system.')

view raw JSON →