Core Text is a
Core Foundation
Core Foundation (also called CF) is a C application programming interface (API) written by Apple Inc. for its operating systems, and is a mix of low-level routines and wrapper functions. Most Core Foundation routines follow a certain naming c ...
style
API
An application programming interface (API) is a connection between computers or between computer programs. It is a type of software interface, offering a service to other pieces of software. A document or standard that describes how to build ...
in
macOS
macOS, previously OS X and originally Mac OS X, is a Unix, Unix-based operating system developed and marketed by Apple Inc., Apple since 2001. It is the current operating system for Apple's Mac (computer), Mac computers. With ...
, first introduced in
Mac OS X 10.4 Tiger, made public in
Mac OS X 10.5 Leopard, and introduced for the
iPad
The iPad is a brand of tablet computers developed and marketed by Apple Inc., Apple that run the company's mobile operating systems iOS and later iPadOS. The IPad (1st generation), first-generation iPad was introduced on January 27, 2010. ...
with iPhone SDK 3.2. Exposing a
C API
An application programming interface (API) is a connection between computers or between computer programs. It is a type of software interface, offering a service to other pieces of software. A document or standard that describes how to build ...
, it replaces the text rendering abilities of the now-deprecated
QuickDraw
QuickDraw was the 2D graphics library and associated application programming interface (API) which is a core part of classic Mac OS. It was initially written by Bill Atkinson and Andy Hertzfeld. QuickDraw still existed as part of the libraries ...
and
ATSUI frameworks in previous versions of Mac OS X. According to Apple, Core Text is "designed for high performance and ease of use" and its layout API is "simple, consistent, and tightly integrated with Core Foundation, Core Graphics, and Cocoa."
Core Text Programming Guide: Core Text Overview
/ref>
Features
Core Text provides the following opaque types:
* CTFramesetter - creates CTFrame objects from given attributed string object and CGPath object using CTTypesetter.
* CTTypesetter - performs line layouts; e.g., line breaking
* CTFrame - represents an array of lines (i.e., CTLine objects).
* CTLine - represents an array of glyph runs.
* CTRun - an ordered collection of glyphs sharing the same attribute.
* CTFont - represents a font.
Example
The following code displays the text "Hello, World!" to the given graphics context in Objective-C
Objective-C is a high-level general-purpose, object-oriented programming language that adds Smalltalk-style message passing (messaging) to the C programming language. Originally developed by Brad Cox and Tom Love in the early 1980s, it was ...
.
// Prepare font
CTFontRef font = CTFontCreateWithName(CFSTR("Times"), 48, NULL);
// Create an attributed string
CFStringRef keys[] = ;
CFTypeRef values[] = ;
CFDictionaryRef attr = CFDictionaryCreate(NULL, (const void **)&keys, (const void **)&values,
sizeof(keys) / sizeof(keys[0]), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFAttributedStringRef attrString = CFAttributedStringCreate(NULL, CFSTR("Hello, World!"), attr);
CFRelease(attr);
// Draw the string
CTLineRef line = CTLineCreateWithAttributedString(attrString);
CGContextSetTextMatrix(context, CGAffineTransformIdentity); //Use this one when using standard view coordinates
//CGContextSetTextMatrix(context, CGAffineTransformMakeScale(1.0, -1.0)); //Use this one if the view's coordinates are flipped
CGContextSetTextPosition(context, 10, 20);
CTLineDraw(line, context);
// Clean up
CFRelease(line);
CFRelease(attrString);
CFRelease(font);
References
External links
Core Text overview
at Apple Developer Connection
{{Mac-stub
Text rendering libraries
macOS APIs