NS在UITableView中使用CATextLayer分配String

时间:2012-06-18 11:49:15

标签: iphone uitableview nsattributedstring catextlayer

我的要求是文本的一部分应该在UITableview的每个单元格中显示为粗体和彩色。我使用NSMutableAttributedString,CATextLayer和cell的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;
        }

问题是,当我快速滚动表格时,文本显示为动画(重叠),停止滚动后显示正常。请找到以下参考图片

Overlapping AttributedString While Table Scrolling Fast

任何人都可以帮助我,为什么只有当我滚动表格时才发生这种重叠?

1 个答案:

答案 0 :(得分:1)

这真是一个有趣的问题,CALayer属性默认为动画(隐式动画),因此它可能是scrollview动画中CATextLayer的默认行为。一种解决方案可能是禁用它们,您有两种选择:

  1. 当前交易中的可动画动画,但即使您正在创建新交易,我也不会如何影响桌面视图上的普通动画

    [CATransaction begin];

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

    [CATransaction commit];

  2. 设置动画动作词典返回null,这是帮助Disabling implicit animations in -[CALayer setNeedsDisplayInRect:]