gpt4 book ai didi

iphone - 描边同时应用于文本和阴影

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:22:49 25 4
gpt4 key购买 nike

我试图简单地用笔画绘制一些文本,然后应用投影,但笔画被应用于投影。我该如何防止这种情况?

Notice stroke is applied to drop shadow

// Setup context
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);

// draw photo into context
CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
// Set Text Font
CGContextSelectFont(context, font.fontName.UTF8String, fontSize, kCGEncodingMacRoman);

// Set Text Drawing Mode
CGContextSetTextDrawingMode(context, kCGTextFillStroke);

// Set text fill color
CGColorRef tmpColor = color.CGColor;
CGFloat newComponents[4] = {};
memcpy(newComponents, CGColorGetComponents(tmpColor), sizeof(newComponents));
CGContextSetRGBFillColor(context, newComponents[0], newComponents[1], newComponents[2], newComponents[3]);

// Calculate Height
UIFont *testFont = [UIFont fontWithName:font.fontName size:fontSize];
CGSize size = [text sizeWithFont:testFont];

// Setup Font Shadow
CGSize shadowSize = CGSizeMake(6, 6);
CGContextSetShadowWithColor(context, shadowSize, 1.0, [[UIColor darkGrayColor] CGColor]);

// Set Stroke Color
CGContextSetRGBStrokeColor(context, 0, 255, 0, 1);
CGContextSetLineWidth(context, 2.0);

// draw text at location centered based on width.
CGContextShowTextAtPoint(context, (w / 2) - (textWidth / 2), h - size.height, ctext, strlen(ctext));

// Render Bitmap
CGImageRef imageMasked = CGBitmapContextCreateImage(context);
UIImage *returnImage = [UIImage imageWithCGImage:imageMasked];

最佳答案

因为您的文本绘制模式是kCGTextFillStrokeCGContextShowTextAtPoint首先绘制填充(并为其生成阴影),然后绘制笔划(它有自己的阴影)。

要修复它,请使用 transparency layer .

请注意,如果您使用 CGContextBeginTransparencyLayerWithRect() 并传入尽可能小的矩形,绘制速度会快得多。

或者:如果它足够好,您可以考虑分两步绘制文本:

  1. 用阴影填充
  2. 中风,没有阴影

如果您的笔划很小,则差异不会很明显,并且绘制速度会快得多。

关于iphone - 描边同时应用于文本和阴影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9816222/

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