2013-03-09 29 views
1

我正在研究Core Plot项目并遇到问题。我的X轴标签都不止一行。我希望它们完全居中,我迄今能够做到的唯一方法是手动向NSString(即标签)添加空格。请参阅下面的代码...在Core Plot中定位多行标签,iOS

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]; 

我认为有一种方法来对齐标签编程,但我还没有找到它。我会很感激任何帮助。这里有一个图表标签的样子

enter image description here

这里就是我定义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]; 

回答

1

这应该工作 - 你应该尝试使用标签上的textAlignment属性:

label.textAlignment = NSTextAlignmentCenter; 

正如@马丁指出,你还可以添加UILineBreakModeWordWrap所以换行符之后才全字出现:

label.lineBreakMode = UILineBreakModeWordWrap; 
+1

并添加NSLinebreakByWordWrapping使换行符与整个单词 – 2013-03-09 01:42:40

+0

对不起,没有工作发生...我已经编辑我的帖子,虽然包括更多的代码。我认为创作标签的方式有些不妥。当我尝试newLabel.textAlignment时,它不起作用。 – pj409 2013-03-09 01:53:28

+0

@ pj409尝试使用labelAlignment = CPAlignmentCenter;并检查此链接 - https://code.google.com/p/core-plot/source/detail?r=cd1f17fc48# - 确保您点击“展开全部” – lifetimes 2013-03-09 01:57:25

0

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

textStyle.textAlignment = CPTTextAlignmentCenter;