【问题标题】:Draw custom shadow on label在标签上绘制自定义阴影
【发布时间】: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


    【解决方案1】:

    没有实现这种效果的内置方法。您必须自己实现绘图代码。

    这里有两个创建阴影的想法:

    • 以深蓝色绘制文本,重复 n 次,从 0.5 pt 的原始位置开始。步骤向右移动。这性能很差,但很容易实现。

    • 使用 Core Text 查找文本轮廓并实现一些算法来创建阴影的实际轮廓。然后可以将其用作shadowPath 属性。

    【讨论】:

    • 是的,谢谢,我实际上正朝着您的第二个选择前进,但是找到一种知道确切形状的方法有点棘手。我需要我的标签在表格视图中,重复多次,所以你的第一个选项充其量是有趣的:P
    • @Zil 你真的尝试过第一个解决方案吗?这似乎很简单,毕竟也许还没有那么糟糕。就代码大小和复杂性而言,计算拉伸路径肯定更复杂。
    • 我还没试过,我读错了你的第一步。我以为你的意思是复制实际标签 n 次,而不是绘制它。我现在已经设法绘制了我的文本的所有点,我会尝试将它向下复制 n 次,看看它是如何进行的 ^^我的代码很阴暗,但它可以完成这项工作。
    猜你喜欢
    • 1970-01-01
    • 2012-03-03
    • 1970-01-01
    • 1970-01-01
    • 2011-07-29
    • 2010-11-16
    • 2013-06-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多