【问题标题】:How to move to the right the label Core Plot?如何将标签Core Plot向右移动?
【发布时间】:2012-04-26 09:38:42
【问题描述】:

我使用以下方法来显示我的情节的标签:

-(CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)index{ 
 ... 
 CPTTextLayer *label=[[CPTTextLayer alloc] initWithText:stringValue style:textStyle];
 }

每个索引都应该返回标签

我知道可以使用以下方法向上或向下移动标签:

plot.labelOffset=10;

问题是:如何将标签向右移动一点? 我试过用

label.paddingLeft=50.0f;

但它不起作用。

【问题讨论】:

    标签: xcode core-plot padding


    【解决方案1】:

    在您的示例中添加填充确实有效,但可能不是您期望的方式。散点图和条形图将使每个数据点上方的标签居中(具有正偏移)。填充使整个标签更宽,因此当居中时,测试会偏向一边。很难控制,尤其是标签文本的长度不同时。

    有一个悬而未决的问题需要解决 (issue 266)。无法保证何时修复,但我们正在研究。

    【讨论】:

    • 遇到同样的问题。想把Label移到左边。现在有解决办法吗?
    • 在标签文本层内增加paddingRight
    【解决方案2】:

    我遇到了同样的问题并想出了一个不同的解决方案。

    我决定做的是使用 CPTAxisLabel 方法 initWithContentLayer 创建标签:

    CPTTextLayer *textLayer = [[CPTTextLayer alloc] initWithText:labelStr  style:axisTextStyle];
    
    CGSize textSize = [textLayer sizeThatFits];
    
    // Calculate the padding needed to center the label under the plot record.
    textLayer.paddingLeft = barCenterLeftOffset - textSize.width/2.0;
    
    CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithContentLayer:textLayer];
    

    这里的barCenterLeftOffset是绘图记录中心的偏移量。

    我写了一篇关于这个的文章:

    http://finalize.com/2014/09/18/horizontal-label-positioning-in-core-plot-and-other-miscellaneous-topics/

    可以在以下位置找到我创建的使用此解决方案的演示项目:

    https://github.com/scottcarter/algorithms

    【讨论】:

      【解决方案3】:

      您可以继承 CPTTextLayer 并包含偏移量。

      @interface WPTextLayer : CPTTextLayer
      
      @property (nonatomic) CGPoint offset;
      
      @end
      
      @implementation WPTextLayer
      
      -(void)setPosition:(CGPoint)position
      {
          CGPoint p = CGPointMake(position.x + self.offset.x, position.y + self.offset.y);
          [super setPosition:p];
      }
      

      然后使用:

      WPTextLayer *tLayer = [[WPTextLayer alloc] initWithText:@"blah" style:textStyle];
      tLayer.offset = CGPointMake(3, -3);
      return tLayer;
      

      这可能会产生我不知道的后果,但到目前为止它似乎正在发挥作用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多