私の要件は、テキストの一部をUITableviewの各セル内に太字で色付きで表示することです。NSMutableAttributedString、CATextLayer、およびセルのcontenviewのレイヤーを使用して管理しました。以下は私が表示するために使用したコードです。
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;
}
問題は、テーブルを高速でスクロールすると、テキストがアニメーション化(オーバーラップ)として表示され、スクロールを停止した後、正しく表示されることです。以下の参考画像をご覧ください
テーブルをスクロールしたときにのみこの重複が発生する理由を教えてください。