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)
Use sizeWithFont:constrainedToSize:lineBreakMode:
[someString sizeWithFont:yourFont
constrainedToSize:CGSizeMake(maxWidthYouSpecify, CGFLOAT_MAX)
lineBreakMode:UILineBreakModeWordWrap];
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));
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())
}
//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)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With