2011-10-07 2 views
1

J'essaie d'afficher des textes dans un CGContext et je veux savoir comment emballer les textes.Envelopper le texte dans CGContextShowTextAtPoint

Comment puis-je rendre cela possible?

Voici mon code:

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)); 

}

Répondre

0

Si vous avez besoin de mise en forme de texte plus avancé (comme l'emballage), utilisez Core Text à la place. Pour afficher une chaîne simple dans un rectangle, utilisez

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