Created
April 15, 2011 14:53
-
-
Save jweinberg/921822 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)drawRect:(CGRect)rect; | |
{ | |
CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, CGRectGetHeight(self.bounds)); | |
CTFramesetterRef ref = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)self.attributedString); | |
UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.bounds]; | |
CTFrameRef ctFrame = CTFramesetterCreateFrame(ref, CFRangeMake(0, 0), [path CGPath], NULL); | |
CGContextRef ctx = UIGraphicsGetCurrentContext(); | |
CGContextConcatCTM(ctx, flipVertical); | |
//This one works fine | |
//CTFrameDraw(ctFrame, ctx); | |
NSMutableArray *lines = [NSMutableArray arrayWithArray:(id)(id)CTFrameGetLines(ctFrame)]; | |
CGPoint * origins = malloc(sizeof(CGPoint) * [lines count]); | |
//Line truncation code | |
CTLineRef lastLine = (CTLineRef)[lines lastObject]; | |
CFStringRef str = CFSTR("\u2026"); | |
CFAttributedStringRef truncationString = (CFAttributedStringRef)[[[NSAttributedString alloc] initWithString:(id)str] autorelease]; | |
CTLineRef truncationToken = CTLineCreateWithAttributedString(truncationString); | |
NSRange lastLineRange = NSMakeRange(CTLineGetStringRange(lastLine).location, 0); | |
lastLineRange.length = [self.attributedString length] - lastLineRange.location; | |
CFAttributedStringRef longString = (CFAttributedStringRef)[self.attributedString attributedSubstringFromRange:lastLineRange]; | |
CTLineRef longLine = CTLineCreateWithAttributedString(longString); | |
CTLineRef truncated = CTLineCreateTruncatedLine(longLine, CGRectGetWidth(self.bounds), kCTLineTruncationEnd, truncationToken); | |
CFRelease(truncationToken); | |
if (truncated) | |
{ | |
[lines replaceObjectAtIndex:[lines count]-1 withObject:(id)truncated]; | |
CFRelease(truncated); | |
} | |
#warning Comment out this line to fix the truncation problem | |
//!!!!!!!!!!!!!! | |
CFRelease(longLine); | |
//!!!!!!!!!!!!!!! | |
CTFrameGetLineOrigins(ctFrame, CFRangeMake(0, 0), origins); | |
for (int i = 0; i < [lines count]; ++i) | |
{ | |
CGPoint origin = origins[i]; | |
CGContextSetTextPosition(ctx, origin.x ,origin.y); | |
CTLineRef line = (CTLineRef)[lines objectAtIndex:i]; | |
//This works fine | |
//CTLineDraw(line, ctx); | |
NSArray *runs = (id)CTLineGetGlyphRuns(line); | |
for (id t in runs) | |
{ | |
CTRunRef run = (CTRunRef)t; | |
CTRunDraw(run, ctx, CFRangeMake(0, 0)); | |
} | |
} | |
free(origins); | |
CFRelease(ctFrame); | |
CFRelease(ref); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment