いくつかの UILabels を作成し、それらの .layers を CALayer に追加するループがあります。後で提示する UIImage のコンテキストにオフスクリーンでレンダリングしています (ところで、メインに戻す前に別のキューでこのレンダリングを行っています。 )
次のようにループで実行すると:
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 ではなく 1 つを作成します (同じコード):
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];
そして、そこにあります!何が起こっている?
これらの可変数を持つことができるように、ループが必要です。