Created
January 10, 2013 02:46
-
-
Save anonymous/4498988 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 | |
{ | |
[super drawRect:rect]; | |
CGRect theRect = rect; | |
// create a naked string | |
NSString *string = self.text; | |
CGFloat widthValue = 8.0; | |
CFNumberRef strokeWidth = CFNumberCreate(NULL,kCFNumberFloatType,&widthValue); | |
UIFont *uiFont = [UIFont annotationFont]; | |
CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)uiFont.fontName, uiFont.pointSize, NULL); | |
// pack it into attributes dictionary | |
NSDictionary *attributesDict = [NSDictionary dictionaryWithObjectsAndKeys: | |
(__bridge id)(font), (id)kCTFontAttributeName, | |
strokeWidth, kCTStrokeWidthAttributeName, | |
[UIColor blackColor].CGColor, kCTStrokeColorAttributeName, | |
nil]; | |
// make the attributed string | |
NSMutableAttributedString *stringToDraw = [[NSMutableAttributedString alloc] initWithString:string | |
attributes:attributesDict]; | |
//set center aligned | |
CTTextAlignment alignment = kCTCenterTextAlignment; | |
CTLineBreakMode linebreakmode = kCTLineBreakByCharWrapping; | |
CTParagraphStyleSetting setting[] = { | |
{kCTParagraphStyleSpecifierAlignment, sizeof(alignment), &alignment}, | |
{kCTParagraphStyleSpecifierLineBreakMode, sizeof(linebreakmode), &linebreakmode} | |
}; | |
CTParagraphStyleRef style = CTParagraphStyleCreate(setting, 2); | |
CFRange lastLineRange = CFRangeMake(0, [stringToDraw length]); | |
CFAttributedStringSetAttribute((__bridge CFMutableAttributedStringRef)(stringToDraw), lastLineRange, kCTParagraphStyleAttributeName, style); | |
//vertical align center | |
CGRect boundingBox = CTFontGetBoundingBox(font); | |
float midHeight = theRect.size.height / 2; | |
midHeight -= boundingBox.size.height / 2; | |
// now for the actual drawing | |
CGContextRef context = UIGraphicsGetCurrentContext(); | |
// flip the coordinate system | |
CGContextSetTextMatrix(context, CGAffineTransformIdentity); | |
CGContextTranslateCTM(context, 0, self.bounds.size.height); | |
CGContextScaleCTM(context, 1.0, -1.0); | |
//draw the text | |
CGMutablePathRef path = CGPathCreateMutable(); // 5-2 | |
CGPathAddRect(path, NULL, CGRectMake(0, midHeight - 1/*Core Text is off by 1 px*/, rect.size.width, boundingBox.size.height)); | |
// CGPathAddRect(path, NULL, theRect); // 6-2 | |
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)stringToDraw); // 7-2 | |
CTFrameRef theFrame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, [stringToDraw length]), path, NULL); // 8-2 | |
CFRelease(framesetter); // 9-2 | |
CFRelease(path); // 10-2 | |
CTFrameDraw(theFrame, context); // 11-2 | |
CFRelease(theFrame); // 12-2 | |
CFRelease(strokeWidth); | |
CFRelease(font); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment