-1

I am trying to create a CATextLayer with a frame that is the exact size of 3 lines of text + the spacing in between this text. I know that my font size is 12.

The height of the frame should therefore be 12*3 + 2*spaceSize.

I want the number that spaceSize is equal to, I can't find it anywhere.


The font I'm using is Helvetica if that helps.

Sam
  • 1,765
  • 11
  • 82
  • 176
  • Why not use the API to calculate the frame required to fit the text for a given font? `NSAttributedText boundingRect` will give you the size. Be sure to include the proper attributes such as font and paragraph style (if not default). – rmaddy Apr 24 '19 at 02:10
  • @rmaddy I want to set a max width, and some other things. This is a toy example. – Sam Apr 24 '19 at 04:49
  • Your comment doesn't change mine. – rmaddy Apr 24 '19 at 04:56

1 Answers1

4
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString( (CFMutableAttributedStringRef) textLayer.string);
CGSize suggestedSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, 0), NULL, CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX), NULL);
CFRelease(framesetter);
CGFloat layerHeight = ceil(suggestedSize.height);
spaceSize = (layerHeight - 36) / 2.0;
Arsil
  • 51
  • 8