gpt4 book ai didi

ios - CALayer 上的绘制文本在运行时不显示或崩溃

转载 作者:可可西里 更新时间:2023-11-01 05:56:05 33 4
gpt4 key购买 nike

我有一个 CALayer,我先在上面画一些东西,然后画一个文本:

- (void)drawInContext:(CGContextRef)context
{
CGContextSaveGState(context);

// draw things, everything displays correctly ...

CGSize expectedCreditSize = [[gpData.credits stringValue] sizeWithFont:[UIFont
systemFontOfSize:self.fontSize]];

rect = CGRectMake(self.bounds.origin.x, self.bounds.size.height/2,
expectedCreditSize.width, expectedCreditSize.height);

CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]);

[self.creditString drawInRect:rect
withFont:[UIFont systemFontOfSize:self.fontSize]
lineBreakMode:NSLineBreakByWordWrapping
alignment:NSTextAlignmentRight];

CGContextRestoreGState(context);
}

所有图形都正确显示,但文本根本不显示。我做错了什么?

我还尝试将 CATextLayer 添加为子层,但因此在运行时收到错误消息。

CATextLayer *creditsTextLayer = [[CATextLayer alloc] init];
[creditsTextLayer setFrame:self.frame];
[creditsTextLayer setPosition:self.position];
[creditsTextLayer setString:self.creditString];
[creditsTextLayer setFontSize:self.fontSize];
[creditsTextLayer setAlignmentMode:kCAAlignmentLeft];
[creditsTextLayer setForegroundColor:[[UIColor whiteColor] CGColor]];
[self addSublayer:creditsTextLayer];

唯一有效的是这个解决方案:

CGContextSelectFont (context,
"Helvetica-Bold",
self.fontSize,
kCGEncodingMacRoman);
CGContextSetTextDrawingMode (context, kCGTextFill);

CGContextSetRGBFillColor (context, 0, 1, 0, .5);
CGContextSetRGBStrokeColor (context, 0, 0, 1, 1);
CGContextShowTextAtPoint (context, 40, 0, self.creditString, 9);

但这很不舒服。

有没有人知道我可以做些什么来提出我的第一个工作建议?我是否缺少显示文本的内容?

提前致谢!

编辑:

基于 Seamus 的回答,我现在可以正常工作了

- (void)drawInContext:(CGContextRef)context
{
CGContextSaveGState(context);

// draw things like circles and lines,
// everything displays correctly ...

// now drawing the text
UIGraphicsPushContext(context);

CGSize expectedCreditSize = [[gpData.credits stringValue] sizeWithFont:[UIFont
systemFontOfSize:self.fontSize]];

rect = CGRectMake(self.bounds.origin.x, self.bounds.size.height/2,
expectedCreditSize.width, expectedCreditSize.height);

CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]);

[self.creditString drawInRect:rect
withFont:[UIFont systemFontOfSize:self.fontSize]
lineBreakMode:NSLineBreakByWordWrapping
alignment:NSTextAlignmentRight];

UIGraphicsPopContext();
CGContextRestoreGState(context);
}

这意味着我只是推送和弹出文本的图形上下文。这有意义吗?为什么它适用于绘制其他 CGContext 内容?也许有人可以解释......

最佳答案

请注意 -drawInRect:withFont:lineBreakMode:alignment: 不采用 CGContextRef 参数——它绘制到“当前”图形上下文。您没有向我们展示您是如何获得要绘制的上下文的,但通常 NSString 方法会在 -drawRect 方法中使用,并获得如下上下文:

CGContextRef context = UIGraphicsGetCurrentContext();

您可以使用 UIGraphicsPushContext() 使您的上下文成为“当前上下文”(但请确保您还调用了 UIGraphicsPopContext()):

- (void)drawInContext:(CGContextRef)context
{
UIGraphicsPushContext(context);
CGContextSaveGState(context);

// draw things, everything displays correctly ...

CGSize expectedCreditSize = [[gpData.credits stringValue] sizeWithFont:[UIFont
systemFontOfSize:self.fontSize]];

rect = CGRectMake(self.bounds.origin.x, self.bounds.size.height/2,
expectedCreditSize.width, expectedCreditSize.height);

CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]);

[self.creditString drawInRect:rect
withFont:[UIFont systemFontOfSize:self.fontSize]
lineBreakMode:NSLineBreakByWordWrapping
alignment:NSTextAlignmentRight];

CGContextRestoreGState(context);
UIGraphicsPopContext();
}

关于ios - CALayer 上的绘制文本在运行时不显示或崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15815443/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com