Question

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;
Was it helpful?

Solution

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top