ArthurGuibert / FSInteractiveMap

A charting library to visualize and interact with a vector map on iOS. It's like Geochart but for iOS!

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is that possible to add a text on each state ?

davamp opened this issue · comments

  • (void)loadMap:(NSString*)mapName withColors:(NSDictionary*)colorsDict
    {
    _svg = [FSSVG svgWithFile:mapName];

    for (FSSVGPathElement* path in _svg.paths) {
    // Make the map fits inside the frame
    float scaleHorizontal = self.frame.size.width / _svg.bounds.size.width;
    float scaleVertical = self.frame.size.height / _svg.bounds.size.height;
    float scale = MIN(scaleHorizontal, scaleVertical);

      CGAffineTransform scaleTransform = CGAffineTransformIdentity;
      scaleTransform = CGAffineTransformMakeScale(scale, scale);
      scaleTransform = CGAffineTransformTranslate(scaleTransform,-_svg.bounds.origin.x, -_svg.bounds.origin.y);
      
      UIBezierPath* scaled = [path.path copy];
      [scaled applyTransform:scaleTransform];
      
      CAShapeLayer *shapeLayer = [CAShapeLayer layer];
      shapeLayer.path = scaled.CGPath;
      
      // Setting CAShapeLayer properties
      shapeLayer.strokeColor = self.strokeColor.CGColor;
      shapeLayer.lineWidth = 0.5;
      
      
      if(path.fill) {
          if(colorsDict && [colorsDict objectForKey:path.identifier]) {
              UIColor* color = [colorsDict objectForKey:path.identifier];
              shapeLayer.fillColor = color.CGColor;
          } else {
              shapeLayer.fillColor = self.fillColor.CGColor;
          }
          
      } else {
          shapeLayer.fillColor = [[UIColor clearColor] CGColor];
      }
      
      
      
          CATextLayer* text = [CATextLayer new];
          text.string =  [NSString stringWithFormat:@"%@", path.identifier];
          //text.font = (__bridge CFTypeRef _Nullable)([UIFont fontWithName:@"akaChen" size:42]);
          text.font = (__bridge CFTypeRef _Nullable)([UIFont boldSystemFontOfSize:7]);
          text.fontSize = 7;
          text.frame = CGRectMake((CGRectGetMidX(scaled.bounds) - scaled.bounds.size.width/4 ),CGRectGetMidY(scaled.bounds),scaled.bounds.size.width,scaled.bounds.size.height);
        //  text.position = CGPointMake(CGRectGetMidX(scaled.bounds) ,CGRectGetMidY(scaled.bounds) );
    

// CGFloat vert = CGRectGetMidY(scaled.bounds) / CGRectGetHeight(scaled.bounds);

// text.anchorPoint = CGPointMake(0.5, vert );
// text.alignmentMode = kCAAlignmentCenter;
text.foregroundColor = [[UIColor blackColor] CGColor];

    [shapeLayer addSublayer:text];

    [self.layer addSublayer:shapeLayer];
    [_scaledPaths addObject:scaled];
}

}