PyObjC CoreText Framework Bindings
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
- breaking PyObjC has periodically dropped support for older Python versions. Version 12.0 dropped support for Python 3.9, and version 11.0 dropped support for Python 3.8. Ensure your Python version meets the minimum requirement (currently Python 3.10+ for v12.x).
- breaking The behavior of `__init__` methods in Python subclasses of Objective-C classes, particularly when a custom `__new__` method is present, changed in v10.3 and was partially reverted in v10.3.1. In v10.3, `__init__` could not be used when PyObjC provided the `__new__`. In v10.3.1, this was relaxed for user-defined `__new__` methods, but `__init__` still cannot be used with PyObjC's default `__new__`.
- breaking PyObjC v11.1 changed its Automatic Reference Counting (ARC) behavior for methods in the 'init' family to align with `clang`'s documentation. This means that such methods now correctly 'steal' a reference to `self` and return a new reference, which might alter object lifecycle if your code relied on the previous PyObjC behavior.
- gotcha PyObjC's support for free-threading (PEP 703) introduced in Python 3.13 is experimental as of v11.0. While efforts have been made to support it, it may not be fully stable or correct for all use cases. It was explicitly stated as 'not supported' in earlier 3.13-compatible PyObjC releases (v10.3).
Install
-
pip install pyobjc-framework-coretext
Imports
- CTFontCreateWithFontDescriptor
from CoreText import CTFontCreateWithFontDescriptor
- CTFontDescriptorCreateWithNameAndSize
from CoreText import CTFontDescriptorCreateWithNameAndSize
Quickstart
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.')