在CGContextShowTextAtPoint中换行文本

问题描述 投票:1回答:1

我正在尝试在CGContext中显示文本,我想知道如何包装文本。我怎样才能做到这一点?

这是我的代码:

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

}
objective-c iphone cgcontext tex
1个回答
1
投票

如果您需要更高级的文本格式(如包装),请改用Core Text。要在矩形中显示简单字符串,请使用

CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attrString);
CGPath p = CGPathCreateMutable();
CGPathAddRect(p, rect);
CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0,0), p, NULL);
CTFrameDraw(frame, context);
© www.soinside.com 2019 - 2024. All rights reserved.