3
我想在我的CATextLayer中使用一個約束來垂直居中文本,但是這個約束似乎沒有做任何事情。約束設置是錯誤的還是有其他方式我應該做的事情垂直居中文本?在一個CATextLayer中垂直居中文本
CATextLayer *label = [[CATextLayer alloc] init];
NSRect labelRect;
labelRect.origin.x = 20;
labelRect.origin.y = 0;
labelRect.size.width = width - 40;
labelRect.size.height = height;
[label setFont:@"Helvetica-Bold"];
[label setFontSize: fontSize];
[label setFrame:labelRect];
[label setString:[NSString stringWithFormat:@"Menu Item %d", i]];
[label setAlignmentMode:kCAAlignmentLeft];
[label setForegroundColor:whiteColor];
label.layoutManager = [CAConstraintLayoutManager layoutManager];
[label addConstraint:[CAConstraint
constraintWithAttribute:kCAConstraintMidY
relativeTo:@"superlayer"
attribute:kCAConstraintMidY]];
[label setNeedsLayout];
[menuItemLayer addSublayer:label];