【发布时间】:2015-09-01 12:59:22
【问题描述】:
我正在尝试实现这种阴影,我的研究导致我使用CGPathRef自己绘制阴影,但我无法弄清楚它实际上是如何工作的。
绘制label.layer.shadowPath 看起来是一个不错的计划,谁能告诉我/指出我应该如何进行?
编辑:我现在要尝试根据当前标签中的字符串绘制 UIBezierPath,路径是我需要的阴影的实际形状。我不确定这是最好的选择,但它看起来更有希望。
编辑 2:这是我现在正在使用的代码。这将标签的文本概述为图像,但它与标签本身的文本几乎完全相同,我仍然需要努力使它看起来像一个阴影。注意,我们使用的是Xamarin
public override void Draw (CoreGraphics.CGRect rect)
{
base.Draw (rect);
using (CGContext g = UIGraphics.GetCurrentContext ()) {
UIBezierPath completePath = UIBezierPath.Create ();
g.ScaleCTM (1, -1);
g.TranslateCTM (2, -(this.Bounds.Height / 2) - (this.Font.LineHeight / 3));
CTLine line = new CTLine (this.AttributedText);
CTRun[] runs = line.GetGlyphRuns ();
for (int i = 0; i < runs.Length; i++) {
CTRun run = runs [i];
CTFont font = run.GetAttributes ().Font;
for (int j = 0; j < run.GlyphCount; j++) {
NSRange currentRange = new NSRange (j, 1);
CGPoint[] positions = run.GetPositions (currentRange);
ushort[] glyphs = run.GetGlyphs (currentRange);
CGPath letter = font.GetPathForGlyph (glyphs [0]);
CGAffineTransform transform = CGAffineTransform.MakeTranslation (positions [0].X, positions [0].Y);
CGPath path = new CGPath (letter, transform);
UIBezierPath newPath = UIBezierPath.FromPath (path);
completePath.AppendPath (newPath);
}
}
completePath.LineWidth = 1;
UIColor.Red.SetStroke ();
UIColor.Blue.SetFill ();
completePath.Stroke ();
completePath.Fill ();
completePath.ClosePath ();
//Here I will try to loop over my current points and go down & right at every step of the loop, see how it goes performance-wise. Instead of one complex drawing I'll just have a very simple drawing that has thousands of points :o
g.AddPath (completePath.CGPath);
g.DrawPath (CGPathDrawingMode.FillStroke);
}
【问题讨论】:
标签: ios xamarin uilabel shadow uibezierpath