【问题标题】:Is there a way to get the shadowPath for a UILabel text?有没有办法获取 UILabel 文本的 shadowPath?
【发布时间】:2011-10-04 12:14:53
【问题描述】:

我目前正在使用 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;

【问题讨论】:

  • 您好 bclaessens,您找到解决方案了吗?感谢您的回复!

标签: ios uilabel shadowpath


【解决方案1】:

我找到了一个对我来说非常有用的解决方案。只需像在这个示例中一样从标签制作一个图像:

            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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-26
    • 2015-12-16
    • 1970-01-01
    • 2010-11-24
    相关资源
    最近更新 更多