简体   繁体   English

在标签上绘制自定义阴影

[英]Draw custom shadow on label

标签示例

I'm trying to achieve this kind of shadow, my research led me to using the CGPathRef to draw the shadow myself, but I can't figure out how it actually works. 我正在尝试实现这种阴影,我的研究使我不得不使用CGPathRef自己绘制阴影,但是我无法弄清楚它是如何工作的。

Drawing the label.layer.shadowPath looks like a good plan, can anyone show me/point me to how I should proceed? 绘制label.layer.shadowPath看起来是一个不错的计划,任何人都可以告诉我/指出我应该如何进行?

EDIT : I'm now to the point of trying to draw a UIBezierPath based on the string in the current label, the path being the actual shape of the shadow I need. 编辑:我现在到了试图根据当前标签中的字符串绘制UIBezierPath的地步,该路径是我所需阴影的实际形状。 I'm not sure that's the best option but it looks more promising. 我不确定这是最好的选择,但看起来更有希望。

EDIT 2 : Here is the code i'm now working with. 编辑2:这是我现在正在使用的代码。 this outlines the text of the label as an image, but it's pretty much the exact same text as the label itself, i still have to work my way around making it look like a shadow. 这将标签的文本概述为图像,但几乎与标签本身完全相同,但我仍然必须设法使它看起来像阴影。 Note, we're using Xamarin 注意,我们正在使用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);

            }

There is no built in way to achieve this effect. 没有内置的方法来实现此效果。 You have to implement the drawing code on your own. 您必须自己实现绘图代码。

Here are two ideas to create the shadow: 这是创建阴影的两个想法:

  • Draw the text in dark blue color, repeated n times, starting from the original position in 0.5 pt. 从0.5 pt的原始位置开始,以深蓝色绘制文本,重复n次。 steps shifted down right. 步骤右移。 This has bad performance but is really easy to implement. 这虽然性能不好,但实际上很容易实现。

  • Find the text outline using Core Text and implement some algorithm that creates the actual outline of the shadow. 使用“核心文字”查找文字轮廓,并实施一些算法来创建阴影的实际轮廓。 This could then be used as the shadowPath property. 然后可以将其用作shadowPath属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM