Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm adding a UILabel to each cell in my table view. This presents no problem initially. When I round the corners of the UILabel using layer.cornerRadius scrolling the table view grinds to a halt.

 UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(cell.bounds.origin.x+10 ,5, 30, 30)];
 label1.backgroundColor = ([[managedObject valueForKey:@"color"] hasPrefix:@"FFFFFF"]) ? [UIColor blackColor] : color;
 label1.layer.cornerRadius = 10.0f;
 [cell addSubview:label1];
like image 936
yesimarobot Avatar asked Feb 13 '10 02:02

yesimarobot


2 Answers

There is no need for an image with rouned corners - see the answer by fknrdcls to this question:

UILabel layer cornerRadius negatively impacting performance

Basically, you simply need to give your label a transparent background, and add the backgroundcolor to the layer instead. Then, you can disable maskToBounds, which greatly improves performance. So your code becomes:

UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(cell.bounds.origin.x+10 ,5, 30, 30)];
label1.backgroundColor = [UIColor clearColor];
label1.layer.backgroundColor=[UIColor whiteColor].CGColor;
label1.layer.cornerRadius = 10.0f;
label1.layer.masksToBounds = NO;
label1.layer.shouldRasterize = YES;
like image 98
ChristophK Avatar answered Sep 22 '22 20:09

ChristophK


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.

like image 39
nevan king Avatar answered Sep 23 '22 20:09

nevan king