菜鸟教程小白 发表于 2022-12-11 22:15:28

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


                                            <p><p>我试图简单地用笔画绘制一些文本,然后应用阴影,但笔画正在应用于阴影。如何防止这种情况发生?</p>

<p> <img src="/image/LZ9CI.png" alt="Notice stroke is applied to drop shadow"/> </p>

<pre><code>// 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 = {};
memcpy(newComponents, CGColorGetComponents(tmpColor), sizeof(newComponents));
CGContextSetRGBFillColor(context, newComponents, newComponents, newComponents, newComponents);

// Calculate Height
UIFont *testFont = ;
CGSize size = ;

// Setup Font Shadow
CGSize shadowSize = CGSizeMake(6, 6);
CGContextSetShadowWithColor(context, shadowSize, 1.0, [ 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 = ;
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>因为你的文字绘制模式是<code>kCGTextFillStroke</code>,<code>CGContextShowTextAtPoint</code>是先绘制填充(并为其生成阴影),然后绘制描边(得到自己的阴影)。 </p>

<p>要修复它,请使用 <a href="http://developer.apple.com/library/mac/documentation/graphicsimaging/Conceptual/drawingwithquartz2d/dq_trans_layers/dq_trans_layers.html#//apple_ref/doc/uid/TP30001066-CH210-TPXREF101" rel="noreferrer noopener nofollow">transparency layer</a> .</p>

<p>请注意,如果您使用 <code>CGContextBeginTransparencyLayerWithRect()</code> 并传入尽可能小的矩形,它会绘制得更快。</p>

<p>或者:如果它是一个足够好的近似值,您可以考虑分两步绘制文本:</p>

<ol>
<li>用阴影填充</li>
<li>描边,没有阴影</li>
</ol>

<p>如果你的笔画很小,差异不会很明显,而且会画得更快。</p></p>
                                   
                                                <p style="font-size: 20px;">关于iphone - 描边同时应用于文本和阴影,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/9816222/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/9816222/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iphone - 描边同时应用于文本和阴影