Uilabel Layer Cornerradius Negatively Impacting Performance

UILabel layer cornerRadius negatively impacting performance

For labels, or views with rounded corners and or background colors and shadows on scrolling views the solution is pretty simple:

The biggest issue is from the masksToBounds layer option. This appears to tax performance significantly, however the label seems to need this ON to mask the background color to rounded corners. So to get around this you need to set the labels layer background color instead and switch off masksToBounds.

The second issue is that the default behavior is to redraw the view whenever possible which is totally unnecessary with static or slow changing items on scrolling views. Here we simply set layer.shouldRasterize = YES. This will allow CA to 'cache' a rasterized version of the view for quick drawing when scrolling (presumably with hardware acceleration).

You need to make sure your layer has an alpha channel otherwise rasterizing will affect the drawing of rounded corners. I've never had a problem as I have alpha set for my background colors, but you may need to check in your situation.

Here is a sample UILabel set up to work nicely on a scollview:

UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(4, 4, 40.0, 24.0)];
lbl.font = [UIFont fontWithName:@"Helvetica" size:14.0];
lbl.textAlignment = UITextAlignmentRight;
lbl.text = @"Hello World";
// Must set the label background to clear so the layer background shows
lbl.backgroundColor = [UIColor clearColor];
// Set UILabel.layer.backgroundColor not UILabel.backgroundColor otherwise the background is not masked to the rounded border.
lbl.layer.backgroundColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:0.5].CGColor;

lbl.layer.cornerRadius = 8;
lbl.layer.borderColor = [UIColor blackColor].CGColor;
lbl.layer.borderWidth = 1;
// Huge change in performance by explicitly setting the below (even though default is supposedly NO)
lbl.layer.masksToBounds = NO;
// Performance improvement here depends on the size of your view
lbl.layer.shouldRasterize = YES;
lbl.layer.rasterizationScale = [UIScreen mainScreen].scale;
// self here is the child view in the scroll view
[self addSubview:lbl];
[lbl release];

I can fill the iPad 1 screen with views like this and still scroll smoothly :)

Corner Radius property of UILabel is not working in iOS 7.1

Add the next line to your code:

// Swift:
originaltagLbl.layer.masksToBounds = true

// Objective C:
originaltagLbl.layer.masksToBounds = YES;

For information see this SO answer, or read documentation.

UITableViewCell calling .layer.maskToBounds = YES in subview performance issue

First try to set this,

self.layer.masksToBounds = YES;

or if this solution does not work then try following link in which fknrdcls gave very good answer in proper way regarding to protect corner radius negative impact on uitableview.

basically uitableview is subclass of uiscrollview so, below solution might be solve your problem.

UILabel layer cornerRadius negatively impacting performance

CornerRadius sometimes affect the perfomance of uitableview.

UITableView comes to a crawl when adding a UILabel with cornerRadius to each cell

I've tried this before myself and found that it's useless for any kind of view that moves. You should create an image with rounded corners and add that as the background of your label instead.

Tableview scrolling lack of performance using cornerRadius, whats alternatives?

Just change the following line

  exampleView.layer.masksToBounds = YES;

To round out the corners in your imageView. See images below.

The tableview scrolling is a issue with view compositing. In addition to suggestions above, this will certainly help -
Set the opaque property of the UITableViewCell's contentView property, and backgroundView property. Go:

  [cell.contentView setOpaque:YES];
[cell.backgroundView setOpaque:YES];

This will help reduce the view compositing effort at the earliest possible stage.

There are additional, advanced, techniques which would require you to use asynchronous processing to process your images as you scroll. They may be overkill if the above techniques work.

UIImageView with the layer's maskToBounds set to yes
UIImageView with the layer's maskToBounds set to No

iOS UITableView scroll not smoothly after set the layer.cornerRadius of subviews

First of all, please mention bit more , like scenario of your sub-views or rather than setting corner radius what other properties you have set like border-width or masktobound or cliptobound or etc.

secondly,
I am not sure but i had solved my issue with setting just following line

self.layer.masksToBounds = YES;

or if this solution does not work then try following link in which fknrdcls gave very good answer in proper way regarding to protect corner radius negative impact on uitableview.

basically uitableview is subclass of uiscrollview so, below solution might be solve your problem.

UILabel layer cornerRadius negatively impacting performance

How do I create a round cornered UILabel on the iPhone?

iOS 3.0 and later

iPhone OS 3.0 and later supports the cornerRadius property on the CALayer class. Every view has a CALayer instance that you can manipulate. This means you can get rounded corners in one line:

view.layer.cornerRadius = 8;

You will need to #import and link to the QuartzCore framework to get access to CALayer's headers and properties.

Before iOS 3.0

One way to do it, which I used recently, is to create a UIView subclass which simply draws a rounded rectangle, and then make the UILabel or, in my case, UITextView, a subview inside of it. Specifically:

  1. Create a UIView subclass and name it something like RoundRectView.
  2. In RoundRectView's drawRect: method, draw a path around the bounds of the view using Core Graphics calls like CGContextAddLineToPoint() for the edges and and CGContextAddArcToPoint() for the rounded corners.
  3. Create a UILabel instance and make it a subview of the RoundRectView.
  4. Set the frame of the label to be a few pixels inset of the RoundRectView's bounds. (For example, label.frame = CGRectInset(roundRectView.bounds, 8, 8);)

You can place the RoundRectView on a view using Interface Builder if you create a generic UIView and then change its class using the inspector. You won't see the rectangle until you compile and run your app, but at least you'll be able to place the subview and connect it to outlets or actions if needed.

UIView: Rounded Corners Without Performance Issues

I also saw a major performance hit when using cornerRadius on the layer of a view that contained a UIImageView subview. Rasterization solved that problem: view.layer.shouldRasterize = YES;

UIView: Rounded Corners Without Performance Issues

I also saw a major performance hit when using cornerRadius on the layer of a view that contained a UIImageView subview. Rasterization solved that problem: view.layer.shouldRasterize = YES;



Related Topics



Leave a reply



Submit