1

我正在做一个 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];
4

2 回答 2

1

这应该有效 - 您应该尝试使用textAlignment标签上的属性:

label.textAlignment = NSTextAlignmentCenter; 

正如@Martin 所指出的,您还可以添加UILineBreakModeWordWrap这样的换行符,仅在整个单词之后出现:

label.lineBreakMode = UILineBreakModeWordWrap;
于 2013-03-09T01:37:32.113 回答
0

确保创建标签 ( x.labelTextStyle) 时使用的文本样式包括以下属性设置:

textStyle.textAlignment = CPTTextAlignmentCenter;
于 2013-03-09T13:35:17.303 回答