ios - 在 Core Plot, iOS 中居中多行标签

标签 ios objective-c core-plot label

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

我认为有一种方法可以通过编程方式对齐标签,但我还没有找到它。我将不胜感激任何帮助。这是图表标签的屏幕截图

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

最佳答案

这应该可行 - 您应该尝试在标签上使用 textAlignment 属性:

label.textAlignment = NSTextAlignmentCenter; 

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

label.lineBreakMode = UILineBreakModeWordWrap;

关于ios - 在 Core Plot, iOS 中居中多行标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15306171/

相关文章:

ios - 旋转 Xcode 4 中的 iPad 停止屏幕

ios - Objective C 复制属性与复制消息

ios - subview 有 parent 的影子,即使有背景

ios - 向 UIImage 添加宽度?

ios - NSUserDefaults。检索不正确的值

iphone - 使用 webrequest 和 uialertview 在 MonoTouch 中线程化

ios - 我如何在应用程序中接收社交网络新消息的通知

swift - CorePlot 与 cocoa pod 0.38.2

iphone - 如何在核心图中旋转 CPTGraphhostingview

ios - 单击图表点时如何使描述出现 iOS