ios - 将文本放在drawRect中的其他CALayer之上
<p><p>我试图在 <code>CALayer</code> 上方显示文本,但不知何故我无法实现它。我使用了以下代码:</p>
<pre><code>- (void)drawRect:(CGRect)rect
{
self.layer.sublayers = nil;
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSaveGState(ctx);
// Code to show grid lines clipped
CGRect overallRectForBarItem = CGRectMake(20, 20, 176, 35);
CAShapeLayer *barItemShape = ;
barItemShape.frame = overallRectForBarItem;
barItemShape.path = .CGPath;
barItemShape.strokeColor = .CGColor;
barItemShape.fillColor = .CGColor;
;
;
// Other code clipped
CGContextRestoreGState(ctx);
}
-(void) drawTextForBarItem:(NSString*)barItemTitle inRect:(CGRect)rect
{
float actualFontSize = 14.0;
UIFont *font = ;
CGSize size = ;
NSMutableParagraphStyle *paragraphStyle = [[ mutableCopy] autorelease];
paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
paragraphStyle.alignment = NSTextAlignmentRight;
, NSParagraphStyleAttributeName:paragraphStyle}];
}
</code></pre>
<p>以下是模拟器的截图。 <img src="/image/AiGlu.png" alt="Screenshot"/> </p>
<p>我也尝试过<code>zPosition</code>,但没有用。</p>
<pre><code>barItemShape.zPosition = -1000;
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>问题的最可能原因是您创建 <code>CGContextRef</code> 但在需要绘制浅灰色矩形时添加子层。我认为,使用 CoreGraphics 函数绘制矩形并绘制文本会更加一致。</p>
<p>更详细地说,您似乎在当前图层中绘制文本,但在文本上方添加了子图层。</p>
<p>例如,您可以使用此代码尝试一下:</p>
<pre><code>UIColor * redColor = ;
CGContextSetFillColorWithColor(context, redColor.CGColor);
CGContextFillRect(context, self.bounds);
</code></pre></p>
<p style="font-size: 20px;">关于ios - 将文本放在drawRect中的其他CALayer之上,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/23085712/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/23085712/
</a>
</p>
页:
[1]