我正在做一个 Core Plot 项目,遇到了一个问题。我的 X 轴标签都不止一行。我希望它们完全居中,到目前为止我能够做到这一点的唯一方法是手动向作为标签的 NSString 添加空格。请看下面的代码...
NSDictionary *dataTemp=[NSDictionary dictionaryWithObjectsAndKeys:white,@"
White\rBuilding\r(220 max)",rec,@"Rec. Hall\r(240 max)",im,@" IM\rBuilding\r(60
max)",fit,@"Fitness\r Loft\r(40 max)",nil];
我认为有一种方法可以通过编程方式对齐标签,但我还没有找到它。我将不胜感激任何帮助。这是图表标签的屏幕截图
这是我定义 x 轴标签的代码...
//Array containing all the names that will be displayed on the X axis
dates = [NSArray arrayWithObjects:@"Rec. Hall\r(240 max)", @" White\rBuilding\r(220
max)", @" IM\rBuilding\r(60 max)",
@"Fitness\r Loft\r(40 max)", nil];
这是我在图表上制作标签的代码
//X labels
int labelLocations = 0;
NSMutableArray *customXLabels = [NSMutableArray array];
for (NSString *day in dates) {
CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText:day
textStyle:x.labelTextStyle];
newLabel.tickLocation = [[NSNumber numberWithInt:labelLocations] decimalValue];
newLabel.offset = x.labelOffset + x.majorTickLength;
[customXLabels addObject:newLabel];
labelLocations++;
[newLabel release];
}
x.axisLabels = [NSSet setWithArray:customXLabels];
最佳答案
这应该可行 - 您应该尝试在标签上使用 textAlignment
属性:
label.textAlignment = NSTextAlignmentCenter;
正如@Martin 指出的那样,您还可以添加 UILineBreakModeWordWrap
因此换行符仅出现在整个单词之后:
label.lineBreakMode = UILineBreakModeWordWrap;
关于ios - 在 Core Plot, iOS 中居中多行标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15306171/