2011-10-07 32 views
1

我试图在CGContext中显示文本,我想知道如何换行文本。在CGContextShowTextAtPoint中换行文本

我该如何做到这一点?

这里是我的代码:

NSString *text1 = @"These three first articles describe the God in whom we believe. The pictures of the triangle represents the Triune God), of Jesus Christ and of the dove representing the Holy Spirit are grouped together in order to manifest their intimate relationships and unity."; 


- (void) renderPageAtIndex:(NSUInteger)index inContext:(CGContextRef)ctx { 


if(index>[images count]-1)return; 

UIImage *image = [images objectAtIndex:index]; 
CGRect imageRect = CGRectMake(0, 0, image.size.width, image.size.height); 
CGAffineTransform transform = aspectFit(imageRect, 
             CGContextGetClipBoundingBox(ctx)); 

NSString *text1 = [script objectAtIndex:index]; 

char* text = (char *)[text1 cStringUsingEncoding:NSASCIIStringEncoding]; 

CGContextSelectFont(ctx, "Arial", 30, kCGEncodingMacRoman); 

CGContextConcatCTM(ctx, transform); 
CGContextDrawImage(ctx, imageRect, [image CGImage]); 


CGContextSetTextPosition(ctx, 0.0f, round(30/4.0f)); 
CGContextSetTextDrawingMode(ctx, kCGTextFill); 
CGContextSetRGBFillColor(ctx, 0, 0, 0, 1); 
CGContextShowTextAtPoint(ctx,10,10,text, strlen(text)); 

}

回答

0

如果您需要更先进的文本格式(如包装),使用核心文字。要在矩形中显示简单的字符串,请使用

CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attrString); 
CGPath p = CGPathCreateMutable(); 
CGPathAddRect(p, rect); 
CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0,0), p, NULL); 
CTFrameDraw(frame, context);