How to calculate the bounding box of a CATextLayer's string?
Asked Answered
L

4

7

At first sight my question looks really simple, but it seems that I really can't find solution. Here is what it is: I want to calculate the bounding box of a CATextLayer's string. Here is what I do:

CATextLayer *textLayer = [CATextLayer layer];
textLayer.frame = CGRectMake(80, 0.0f, 36.0f, 18.0f);
textLayer.string = @"12";
textLayer.fontSize = [UIFont systemFontSize];
textLayer.foregroundColor = [UIColor whiteColor].CGColor;

NSLog(@"(width,height)=(%f,%f)",
[textLayer.string sizeWithFont:textLayer.font].width,
[textLayer.string sizeWithFont:textLayer.font].height);

The problem is that the output is always : (width,height) = (8.000000,0.000000)

Leeuwenhoek answered 2/3, 2012 at 18:30 Comment(1)
Swift users, if just using one line (maybe more), try myCATextLayer.preferredFrameSize() for a quick win.Loathly
C
7

Use sizeWithFont:constrainedToSize:lineBreakMode:

[someString sizeWithFont:yourFont
       constrainedToSize:CGSizeMake(maxWidthYouSpecify, CGFLOAT_MAX)
           lineBreakMode:UILineBreakModeWordWrap];
Cichocki answered 2/3, 2012 at 18:49 Comment(0)
D
7

As of iOS 7, use NSString's boundingRectWithSize:options:attributes:context. sizeWithFont:constrainedToSize:lineBreakMode: is now deprecated.

CGRect rect = [textLayer.string boundingRectWithSize:textLayer.frame.size options:NSStringDrawingUsesLineFragmentOrigin attributes:nil context:nil];
CGSize size = CGSizeMake(ceilf(rect.size.width), ceilf(rect.size.height));
Dutcher answered 3/1, 2014 at 13:33 Comment(0)
H
2

This should do the trick preferredFrameSize from CATextLayer. Code can go as follow:

func createSampleLabel(coordinate origin: CGPoint) {
   let textLayer = CATextLayer()
   textLayer.string = "some string \n with line breaks and long values...."
   textLayer.frame = .init(origin: origin, size: textLayer.preferredFrameSize())
}

enter image description here

Hystero answered 27/4, 2021 at 15:36 Comment(0)
M
1

//use below function for dynamic CATextLayer

func DynamicLableWidth(reason:NSString,cpT:CGPoint,width:CGFloat,reasonLayer:CATextLayer)
{

    //Dynamic CATextLayer with boundingRect
    let font = UIFont(name: "Arial", size: 20)!
    let attributes: [NSString : AnyObject] = [NSFontAttributeName: font]

    var rect:CGRect = reason.boundingRectWithSize(reasonLayer.frame.size, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: attributes, context: nil)

    var size:CGSize = CGSizeMake(rect.size.width, rect.size.height);
    if cpT.x+20+ceil(size.width)+20>width
    {
        reasonLayer.frame = CGRectMake(cpT.x-20-ceil(size.width)+20,  cpT.y-15, ceil(size.width)+20, 20)
    }
    else
    {
        reasonLayer.frame = CGRectMake(cpT.x+20,  cpT.y-15, rect.size.width+20, 20)
    }
}
Mayor answered 14/9, 2015 at 9:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.