Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)

like image 200
e2l3n Avatar asked Mar 02 '12 18:03

e2l3n


4 Answers

Use sizeWithFont:constrainedToSize:lineBreakMode:

[someString sizeWithFont:yourFont
       constrainedToSize:CGSizeMake(maxWidthYouSpecify, CGFLOAT_MAX)
           lineBreakMode:UILineBreakModeWordWrap];
like image 180
dbrajkovic Avatar answered Oct 26 '22 14:10

dbrajkovic


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));
like image 37
Joony Avatar answered Oct 26 '22 13:10

Joony


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

like image 35
vberihuete Avatar answered Oct 26 '22 15:10

vberihuete


//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)
    }
}
like image 21
Iya Avatar answered Oct 26 '22 15:10

Iya