2016-01-27 11 views
6

Oto moja klasa i to będzie narysować okrąg, wygląda to tak:Jak dodać etykietę lub tekstu w celu CAShapeLayer

enter image description here

class OvalLayer: CAShapeLayer { 

    let animationDuration: CFTimeInterval = 0.3 

    override init() { 
     super.init() 
     fillColor = Colors.green.CGColor 
     path = ovalPathSmall.CGPath 
    } 

    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 


    var ovalPathStart: UIBezierPath { 
     let path = UIBezierPath(ovalInRect: CGRect(x: 0.0, y: 0.0, width: 100.0, height: 100.0)) 

     return path 
    } 
} 

Teraz muszę dodać tekst na środku tego kręgu, próbowałem znaleźć go w google, ale nic, co działa dobrze. Nie jestem pewien, czy to możliwe, czy nie, czy ktoś może mi pomóc, jeśli to możliwe?

+1

Miałeś spojrzenie na "CATextLayer"? https://developer.apple.com/library/prerelease/mac/documentation/GraphicsImaging/Reference/CATextLayer_class/index.html – Hamish

+0

tak, wypróbowałem, nie mogłem znaleźć sposobu dodania podwarstwa z CATextLayer do CAShapeLayer –

Odpowiedz

5

Chyba powinieneś dodać CATextLayer jako podwarstwy do CALayer ... to działa dobrze w ten sposób: spróbuj dodać CAShapeLayer, a potem CATextLayer (aby same CALayer warstwa dominująca), na przykład w następującej kolejności ...

// assume self - is UIView instance 

self.layer.addSublayer(shapedLayer) // shapedLayer - CAShapeLayer instance 
self.layer.addSublayer(textLayer) // textLayer - CATextLayer instance 
+0

dziękuję za odpowiedź, wypróbowałem to, nic się nie pojawia, czy możesz bardziej opisać miejsce? –

+0

@roledeneJKS: Czy możesz skomentować, jak ta odpowiedź zadziałała? – SHN

+0

@roledeneJKS: musisz ustawić ramkę dla textLayer przed dodaniem jej –

1

Wystarczy, że zdobędziesz środek swojego UIBezierPath i dodasz etykietę lub CATextLayer do swojej bieżącej warstwy.

let center : CGPoint = CGPoint(x: CGRectGetMidX(ovalPathStart.bounds), y: CGRectGetMidX(ovalPathStart.bounds)) 

Teraz utwórz UILabel lub CATextLayer i ustawić centrum.

+0

Jestem w stanie stworzyć CATextLayer, ale nie mogę go przenieść. Próbowałem zmienić krawędzie, ramkę, pozycję itp. I po prostu znajduje się w lewym górnym rogu warstwy, do której ją dodałem .... nie chce przesuwać się po mojej grafice. Jak przenieść CATextLayer przez ścieżkę Beziera? – Dewey

2

Swift 3,0

let label = UILabel() 
label.font = UIFont(name: "Helvetica-Bold", size: 12) 
label.frame = CGRect(x: OvalLayer.frame.origin.x + (circleWidth/2), y: OvalLayer.frame.origin.y, width: OvalLayer.bounds.width, height: OvalLayer.bounds.height) 
label.text = "Hello" 
label.textColor = UIColor.red 
label.isHidden = false 

OvalLayer.addSublayer(label.layer) 
+3

To nie działa. Tekst nigdy nie pojawia się (Swift 4/XCode 9) – rommex

0

Tekst na CAShapeLayer Korzystanie CATextLayer

jestem tutaj utworzyć obiektu CAShapeLayer i dodaję CATextLayer do CAShapeLayer Tutaj numberOfArcsCount oznacza jakiś 8

  CAShapeLayer *progressLayer = [[CAShapeLayer alloc] init]; 
      [progressLayer setPath:bezierPath.CGPath]; 


      CATextLayer* text = [CATextLayer new]; 
      for (int i = 1; i <= numberOfArcs.count; i++) { 
      text.string = [NSString stringWithFormat:@"%i", i]; 
      text.font = (__bridge CFTypeRef _Nullable)([UIFont fontWithName:@"akaChen" size:42]); 
      text.font = (__bridge CFTypeRef _Nullable)([UIFont boldSystemFontOfSize:15]); 
      text.fontSize=25; 
      text.frame = CGRectMake(0,0,40,40); 
      text.position = CGPointMake(CGRectGetMidX(progressLayer.frame) ,CGRectGetMidY(progressLayer.frame)); 

      CGFloat vert = CGRectGetMidY(progressLayer.frame)/CGRectGetHeight(text.frame); 

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

      [progressLayer addSublayer:text]; 
     } 
Powiązane problemy