有没有办法获取 UILabel 文本的 ShadowPath?

发布于 2024-12-08 04:57:33 字数 631 浏览 0 评论 0原文

我目前正在使用 UILabel 来显示一些文本。我使用 CALayer 给它一个漂亮的阴影。但是:当我在屏幕上定位(和移动)大量 UILabel 元素时,性能非常糟糕。

我在一些博客文章中读到,设置 CALayer 的 ShadowPath 可以优化性能,我用圆角矩形进行了尝试,它确实有效。所以现在我试图弄清楚如何获取 UILabel 上文本的 ShadowPath。

有人有想法吗?否则我想我将不得不在 coregraphics 中渲染我的文本。

以下是矩形图层上的 ShadowPath 的示例:

        backgroundImage.layer.shadowOffset = CGSizeMake(0, 0);
        backgroundImage.layer.shadowRadius = LIST_TITLE_VIEW_SHADOW_SIZE;
        backgroundImage.layer.shadowOpacity = LIST_TITLE_VIEW_SHADOW_OPACITY;
        backgroundImage.layer.shadowPath = [UIBezierPath bezierPathWithRect:backgroundImage.bounds].CGPath;

I'm currently using a UILabel to display some text. I'm using CALayer to give it a nice shadow. But: performance is really bad when I'm positioning (and moving) a lot of UILabel elements on the screen.

I've read in a few blogposts that setting the shadowPath of a CALayer optimizes the performance, I gave it a shot with a rounded rectangle and it actually works. So now i'm trying to figure out how to get the shadowPath for a text on a UILabel.

Somebody that has an idea? Otherwise I'll have to render my text in coregraphics I guess.

Here's an example with the shadowPath on a rectangle layer:

        backgroundImage.layer.shadowOffset = CGSizeMake(0, 0);
        backgroundImage.layer.shadowRadius = LIST_TITLE_VIEW_SHADOW_SIZE;
        backgroundImage.layer.shadowOpacity = LIST_TITLE_VIEW_SHADOW_OPACITY;
        backgroundImage.layer.shadowPath = [UIBezierPath bezierPathWithRect:backgroundImage.bounds].CGPath;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

李不 2024-12-15 04:57:33

我找到了一个对我来说很有魅力的解决方案。只需从标签中制作一个图像,如本示例所示:

            UILabel employeeName = new UILabel(new RectangleF(7, 49, this.View.Bounds.Width, 50));
            employeeName.Text = "David Johnson";
            employeeName.Font = UIFont.SystemFontOfSize(23);
            employeeName.BackgroundColor = UIColor.Clear;
            employeeName.TextColor = UIColor.White;
            employeeName.Layer.ShadowColor = UIColor.Black.CGColor;
            employeeName.Layer.ShadowOffset = new SizeF(1,1);
            employeeName.Layer.ShadowOpacity = 10;
            employeeName.Layer.ShadowRadius = 0.8f;

            UIGraphics.BeginImageContextWithOptions(employeeName.Bounds.Size, false, 0);
            employeeName.Layer.RenderInContext(UIGraphics.GetCurrentContext());

            UIImageView employeeNameImage = new UIImageView(UIGraphics.GetImageFromCurrentImageContext());
            employeeNameImage.Frame = new RectangleF(7, 49, this.View.Bounds.Width, 50);

            imageView.AddSubview(employeeNameImage);

I´ve found a solution that works like a charm for me. Just make an Image from the label like in this sample:

            UILabel employeeName = new UILabel(new RectangleF(7, 49, this.View.Bounds.Width, 50));
            employeeName.Text = "David Johnson";
            employeeName.Font = UIFont.SystemFontOfSize(23);
            employeeName.BackgroundColor = UIColor.Clear;
            employeeName.TextColor = UIColor.White;
            employeeName.Layer.ShadowColor = UIColor.Black.CGColor;
            employeeName.Layer.ShadowOffset = new SizeF(1,1);
            employeeName.Layer.ShadowOpacity = 10;
            employeeName.Layer.ShadowRadius = 0.8f;

            UIGraphics.BeginImageContextWithOptions(employeeName.Bounds.Size, false, 0);
            employeeName.Layer.RenderInContext(UIGraphics.GetCurrentContext());

            UIImageView employeeNameImage = new UIImageView(UIGraphics.GetImageFromCurrentImageContext());
            employeeNameImage.Frame = new RectangleF(7, 49, this.View.Bounds.Width, 50);

            imageView.AddSubview(employeeNameImage);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文