How to add label or text in to CAShapeLayer
Asked Answered
C

5

10

Here is my class and it'll draw a circle, it looks like this:

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
    }
}

Now I need to add a text to middle of this circle, I tried to find it on google but nothing that works fine. I am not sure if it's possible or not, can anyone help me if it's possible?

Cf answered 27/1, 2016 at 12:45 Comment(2)
Have you had a look at CATextLayer? developer.apple.com/library/prerelease/mac/documentation/…Hort
yes, I tried out, I couldn't able to find how to add sublayer of CATextLayer to CAShapeLayerCf
D
6

I guess you should add CATextLayer as a sublayer to CALayer... That works fine that way: try adding CAShapeLayer first, and then CATextLayer (to same CALayer parent layer), for example in following order...

// assume self - is UIView instance

self.layer.addSublayer(shapedLayer) // shapedLayer - CAShapeLayer instance
self.layer.addSublayer(textLayer) // textLayer - CATextLayer instance
Decompose answered 27/1, 2016 at 13:16 Comment(4)
thank you for your reply, I tried this out, nothing appears, can you more describe place ?Cf
@roledeneJKS: Can you please comment how did this answer work for you?Nicolasanicolau
@roledeneJKS: you have to set frame for textLayer before add itMendenhall
It'd be great if you showed how to create textLayerFarrier
F
6

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)
Fatigued answered 5/12, 2016 at 9:1 Comment(1)
This doesn't work. The text never appears (Swift 4 / XCode 9)Pierrette
M
2

You just need to get the center of your UIBezierPath and add a label or a CATextLayer to your current layer.

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

Now, create a UILabel or CATextLayer and set the center.

Munda answered 28/1, 2016 at 12:55 Comment(2)
I'm able to create a CATextLayer but not move it. I've tried changing bounds, frame, position, etc and it just sits in the top left corner of the layer to which I added it....it refuses to move over my graphic. How do you move the CATextLayer over a Bezier path?Gounod
same issue with me @GounodObedient
C
2

Text on CAShapeLayer Using CATextLayer

Here i am create CAShapeLayer object and i am adding CATextLayer to CAShapeLayer Here numberOfArcsCount means some 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];
        }
Colonist answered 26/3, 2017 at 7:43 Comment(0)
U
1

This way to add label to layer :

labelFrame - CGRect

someString - String

layer - your layer

        let label = CATextLayer()
        label.frame = labelFarme
        label.string = someString
        layer.addSublayer(label)
Ungotten answered 17/7, 2019 at 6:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.