1

我有一个循环制作一些 UILabel 并将它们的 .layers 添加到 CALayer 我正在将屏幕外渲染到我稍后呈现的 UIImage 的上下文中(顺便说一句:我在传递回 main.js 之前在另一个队列上进行此渲染。 )

如果我在这样的循环中这样做:

for (i=0; i<offlineModel.modelArray.count; i++) {

    UILabel *newLabel = [[UILabel alloc] init];
    [newLabel setFont:[UIFont fontWithName:@"Arial-BoldMT" size:9.0]];
    [newLabel setTextColor:[UIColor colorWithRed:76/255.0 green:76/255.0 blue:78/255.0 alpha:1.0]];
    [newLabel setShadowColor:[UIColor whiteColor]];
    [newLabel setShadowOffset:CGSizeMake(1, 1)];
    [newLabel setBackgroundColor:[UIColor greenColor]];
    [newLabel setTextAlignment:UITextAlignmentCenter];
    [newLabel setNumberOfLines:0];

    [newLabel setText:[(LVBarGraphModelPair *)[offlineModel.modelArray objectAtIndex:i] xValue]];
    [newLabel setBounds:CGRectMake(0, 0, barWidth+barSpacing, 3*8.0)];
    [newLabel setAdjustsFontSizeToFitWidth:NO];
    [newLabel sizeToFit];

    CGPoint center = CGPointMake((((offlineModel.modelArray.count-i-1)*barSpacing)+placeholderBarContainerLayerRect.origin.x+(.5*barWidth)), placeholderBarContainerLayerRect.size.height);

    [newLabel setCenter:center];
    [newLabel setFrame:CGRectMake(lroundf(newLabel.frame.origin.x), lroundf(placeholderBarContainerLayerRect.origin.y + placeholderBarContainerLayerRect.size.height+LVBARGRAPH_BAR_ANIMATION_XAXIS_LABELS_OFFSET_X), newLabel.frame.size.width, newLabel.frame.size.height)];

    [layer addSublayer:newLabel.layer];
}

我在标签应该在的地方得到黑色矩形: 黑色矩形而不是标签 因此,为了解决问题,我只需删除循环(相同的代码)就可以制作一个而不是 6 个:

    UILabel *newLabel = [[UILabel alloc] init];
    [newLabel setFont:[UIFont fontWithName:@"Arial-BoldMT" size:9.0]];
    [newLabel setTextColor:[UIColor colorWithRed:76/255.0 green:76/255.0 blue:78/255.0 alpha:1.0]];
    [newLabel setShadowColor:[UIColor whiteColor]];
    [newLabel setShadowOffset:CGSizeMake(1, 1)];
    [newLabel setBackgroundColor:[UIColor greenColor]];
    [newLabel setTextAlignment:UITextAlignmentCenter];
    [newLabel setNumberOfLines:0];

    [newLabel setText:[(LVBarGraphModelPair *)[offlineModel.modelArray objectAtIndex:i] xValue]];
    [newLabel setBounds:CGRectMake(0, 0, barWidth+barSpacing, 3*8.0)];
    [newLabel setAdjustsFontSizeToFitWidth:NO];
    [newLabel sizeToFit];

    CGPoint center = CGPointMake((((offlineModel.modelArray.count-i-1)*barSpacing)+placeholderBarContainerLayerRect.origin.x+(.5*barWidth)), placeholderBarContainerLayerRect.size.height);

    [newLabel setCenter:center];
    [newLabel setFrame:CGRectMake(lroundf(newLabel.frame.origin.x), lroundf(placeholderBarContainerLayerRect.origin.y + placeholderBarContainerLayerRect.size.height+LVBARGRAPH_BAR_ANIMATION_XAXIS_LABELS_OFFSET_X), newLabel.frame.size.width, newLabel.frame.size.height)];

    [layer addSublayer:newLabel.layer];

没有循环它就会出现!

它就在那里!到底是怎么回事?

我需要循环,所以我可以拥有可变数量的这些。

4

1 回答 1

0

您应该尝试使用CATextLayer而不是创建UILabel和使用它们的层。

于 2012-09-16T19:49:28.310 回答