Why is my text drawing from the bottom up?
In my custom view, I have the following code:
-(void) drawRect:(CGRect) rect
{
// Drawing code
CGContextRef context = UIGraphicsGetCurrentContext();
![NSString *myString = @"Hello World, This is 开发者_JS百科for\nhttp://www.stackoverflow.com";
// Prepare font
CTFontRef font = CTFontCreateWithName(CFSTR("Times"), 12, NULL);
// Create an attributed string
CFStringRef keys[] = { kCTFontAttributeName };
CFTypeRef values[] = { font };
CFDictionaryRef attr = CFDictionaryCreate(NULL, (const void **)&keys, (const void **)&values,
sizeof(keys) / sizeof(keys[0]), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFAttributedStringRef attrString = CFAttributedStringCreate(NULL, (CFStringRef)myString, attr);
CFRelease(attr);
// Draw the string
//CTLineRef line = CTLineCreateWithAttributedString(attrString);
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attrString);
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, self.frame);
CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);
//CGContextSetTextMatrix(context, CGAffineTransformIdentity); // this line is wrong, use the below line
CGContextSetTextMatrix(context, CGAffineTransformMakeScale(1.0, -1.0));
CGContextSetTextPosition(context, 20, 12);
CTFrameDraw(frame, context);
// Clean up
CFRelease(path);
CFRelease(frame);
CFRelease(attrString);
CFRelease(font);
}
However, that looks like this:
How can I make the text go down, and also start from the top left?
Here's the copy & paste of one of my drawRect: methods, which does work perfectly.
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSaveGState( ctx );
CGContextSetTextMatrix( ctx, CGAffineTransformIdentity );
CGContextTranslateCTM( ctx, rect.origin.x, rect.origin.y );
CGContextScaleCTM( ctx, 1.0f, -1.0f );
CGContextTranslateCTM( ctx, rect.origin.x, - ( rect.origin.y + rect.size.height ) );
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect( path, NULL, rect );
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString( __labelData.attributedString );
CTFrameRef frame = CTFramesetterCreateFrame( framesetter, CFRangeMake( 0, 0 ), path, NULL );
CTFrameDraw( frame, ctx );
CGPathRelease( path );
CFRelease( frame );
CFRelease( framesetter );
CGContextRestoreGState( ctx );
Just replace __labelData.attributedString
with your attributed string.
精彩评论