Question

My requirement is parts of text should be displayed as bold and colored within each cell of a UITableview. I have managed it using the NSMutableAttributedString, CATextLayer and cell's contenview's layer. Below is the code that i have used to display.

 CATextLayer *textLayer = nil;   //used to draw the attributed string
 CALayer *layer = nil; 
    if(cell == nil) {
                 textLayer = [[CATextLayer alloc] init];
                textLayer.backgroundColor = [UIColor clearColor].CGColor;
                [textLayer setForegroundColor:[[UIColor clearColor] CGColor]];
                [textLayer setContentsScale:[[UIScreen mainScreen] scale]];
                textLayer.wrapped = YES;
                layer = cell.contentView.layer;
                [layer addSublayer:textLayer];
                textLayer = nil;
    }
//get the layer by nesting sublayer of cell layer because we don't have tag property for the layer and textlayer.


if (!layer) {
       layer = cell.contentView.layer;
   }



for (id subLayer in layer.sublayers) {
            if ([subLayer isKindOfClass:[CATextLayer class]]) {
                // NSLog(@"found textlayer");
                textLayer = subLayer;
                textLayer.string = nil;
            }
        }
textLayer.frame = CGRectMake(53.0f, 23.0f, 240.0f, attributedStrHeight);

        if([self isNotNull:mAttrbtdStr]) {
            textLayer.string = mAttrbtdStr;
        }

The Problem is when I scroll the table fast the text is displaying as animated(Overlapping), and after stopped scrolling it is displaying properly. Please find the below reference image

Overlapping AttributedString While Table Scrolling Fast

Could any one please help me why this overlapping is happening only when i scroll the table?

Was it helpful?

Solution

That is really an interesting question, CALayer properties are animated by default(implicit animation) so it could be the default behavior of the CATextLayer inside the scrollview animation. One solution could be disable them, you have two options:

  1. DIsable animation in current transaction, but I don't how this could affect normal animation on tableview even if you are creating a new transaction

    [CATransaction begin];

    [CATransaction setValue: [NSNumber numberWithBool: YES] forKey: kCATransactionDisableActions];

    [CATransaction commit];

  2. Setting the animation action dictionary the return null, here's a help Disabling implicit animations in -[CALayer setNeedsDisplayInRect:]

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top