Skip to content

Instantly share code, notes, and snippets.

@ishikawa
ishikawa / gist:23178
Created November 9, 2008 02:13
Typesetting a simple paragraph by using Core Text
- (void) drawText: (NSRect) theRect {
const CTFramesetterRef framesetter =
CTFramesetterCreateWithAttributedString((CFAttributedStringRef)_text);
// Initialize a rectangular path.
const CGMutablePathRef path = CGPathCreateMutable();
const CGRect rect = CGRectInset(NSRectToCGRect([self bounds]), 5.0, 5.0);
CGPathAddRect(path, NULL, rect);
// Create the frame and draw it into the graphics context
@rsms
rsms / core-text-snippet1.m
Created January 6, 2011 17:16
Some ramblings and cut-outs from wwdc presentation on CoreText
#import <Foundation/Foundation.h>
#import <ApplicationServices/ApplicationServices.h>
CTFontRef font = CTFontCreateWithName(CFSTR("LucidaGrande"), 11.0, NULL);
CFTypeRef keys[] = { kCTFontAttributeName };
CFTypeRef values[] = { font };
CFDictionaryRef attributes =
CFDictionaryCreate(kCFAllocatorDefault,
(const void**)keys,
@rudyjahchan
rudyjahchan / AClassACategoryImplementation.h
Created January 23, 2012 02:42
Monkey-Patching iOS with Objective-C Categories Part I: Simple Extensions and Overrides
#import <Foundation/Foundation.h>
@interface AClass (ACategory)
@end
@jjgod
jjgod / cascadelist.m
Created April 19, 2012 08:32
Use cascade list attribute to customize font fallback in Core Text
#import <ApplicationServices/ApplicationServices.h>
#import <Foundation/Foundation.h>
int main(int argc, char *argv[])
{
if (argc != 2)
return 0;
NSAutoreleasePool *pool = [NSAutoreleasePool new];
CFStringRef name = (CFStringRef) [NSString stringWithUTF8String: argv[1]];
@wess
wess / gist:3136429
Created July 18, 2012 14:14
Get a clipping rect for CoreText
static CGRect clipRectToPath(CGRect rect, CGPathRef path)
{
size_t width = floorf(rect.size.width);
size_t height = floorf(rect.size.height);
uint8_t *points = calloc(width * height, sizeof(*points));
CGContextRef bitmapContext = CGBitmapContextCreate(points, width, height, sizeof(*points) * 8, width, NULL, kCGImageAlphaOnly);
BOOL atStart = NO;
NSRange range = NSMakeRange(0, 0);
NSUInteger x = 0;
@jjgod
jjgod / CTFeature.c
Created July 31, 2012 21:38
Lookup AAT features with Core Text
// CTTest.c
#include <ApplicationServices/ApplicationServices.h>
CFDictionaryRef findFeatureByName(CFArrayRef features, const char *name)
{
CFDictionaryRef feature = NULL;
CFStringRef featureName = CFStringCreateWithCString(NULL, name, kCFStringEncodingUTF8);
CFIndex i;
for (i = 0; i < CFArrayGetCount(features); i++) {
@wess
wess / gist:3278911
Created August 6, 2012 22:12
Tapping in fucking CoreText
// A small chunk of it.
- (void)tapGestureAction:(UITapGestureRecognizer *)gesture
{
CGPoint location = [gesture locationInView:self];
_linkLocation = location;
location.y += (ARTICLE_BODY_FONT_SIZE / lineHeightFactor);
CFArrayRef lines = CTFrameGetLines(_frame);
@piaoapiao
piaoapiao / gist:3347092
Created August 14, 2012 07:04
select text in UIView
http://stackoverflow.com/questions/7979592/is-it-possible-to-select-the-text-rendered-by-core-text-in-iphone
@mkdynamic
mkdynamic / CoreTextLabel.h
Created October 4, 2012 18:13 — forked from dkasper/CoreTextLabel.h
A multiline label drawn with core text. Needed it for line spacing, can easily be modified to use any core text properties though.
//
// CoreTextLabel.h
// Frost
//
// Created by David Kasper on 10/1/12.
//
#import <UIKit/UIKit.h>
#import <CoreText/CoreText.h>
@zsiegel
zsiegel / gist:3939839
Last active October 11, 2015 23:58
Core Text - Line height property inspection
/* Setup our fonts - One system font from Apple and a custom font */
CTFontRef bodyCopyFont = CTFontCreateWithName((__bridge CFStringRef) [Theme fontForBodyOfSize:11].fontName, [Theme fontForBodyOfSize:11].lineHeight, NULL);
CTFontRef systemFont = CTFontCreateWithName((__bridge CFStringRef) [UIFont systemFontOfSize:11].fontName, [UIFont systemFontOfSize:11].lineHeight, NULL);
/* Lets inspect the properties */
NSLog(@"*****************************************************************");
NSLog(@"SYSTEM-FONT LINE HEIGHT PROPERTY %f", [UIFont systemFontOfSize:11].lineHeight);
NSLog(@"SYSTEM-FONT CALCULATED LINE HEIGHT %f", [self getLineHeightForFont:systemFont]); //Custom function
NSLog(@"*****************************************************************");
NSLog(@"CUSTOM-FONT LINE HEIGHT PROPERTY %f", [Theme fontForBodyOfSize:11].lineHeight);