2013-03-09 30 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;