Core Textは、Mac OS X v10.4で初めて導入され、Mac OS X v10.5で公開されたmacOSのCore Foundation風のAPIで、古くからmacOSにあり非推奨となったQuickDrawやATSUIに代わってテキストレンダリングの機能を担うものである。Appleによると、Core Textは高いパフォーマンスと利用の容易さを意識して設計され、このレイアウトAPIはシンプルで安定しており、Core FoundationやCore Graphics、Cocoaと密接に関連している。Core Text Programming Guide: Core Text Overview

特徴 Core Textは次のような不透過型を提供している。 CTFramesetter - CTTypesetterを使って、与えられた属性付き文字列とCGPathからCTFrameオブジェクトを生成する。 CTTypesetter - 改行などの行のレイアウトを行う。 CTFrame - 行(CTLineオブジェクト)の配列を表す。 CTLine - 同じ属性を持つグリフの並びの配列を表す。 CTFont - フォントを表す。

使用例 次のコードは与えられたグラフィックコンテクストに「Hello, World!」と表示する。 // フォントの準備 CTFontRef font = CTFontCreateWithName(CFSTR("Times"), 48, NULL);

// 属性付き文字列の生成 CFStringRef keys[] = { kCTFontAttributeName }; CFTypeRef values[] = { font }; CFDictionaryRef attr = CFDictionaryCreate(NULL, (const void **)&keys, (const void **)&values, sizeof(keys) / sizeof(keys), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); CFAttributedStringRef attrString = CFAttributedStringCreate(NULL, CFSTR("Hello, World!"), attr); CFRelease(attr);

// 文字列の描画 CTLineRef line = CTLineCreateWithAttributedString(attrString); CGContextSetTextMatrix(context, CGAffineTransformIdentity); CGContextSetTextPosition(context, 10, 20); CTLineDraw(line, context);

// 後片付け CFRelease(line); CFRelease(attrString); CFRelease(font);

参照

外部リンク Overview at Apple's Developer Connection