0

Has anyone got a good copyfitting routine for iOS? I basically want to draw the largest text that will fit inside a rectangle. The previous way I did it in Carbon was to create the text at a huge font size, then keep reducing the size until the text fit completely in the box. In Carbon, there was a way I could tell that the text overflowed the rect. I've looked through the CoreText definition, and the UITextView definitions, and haven't found where this information is. I'm about to look through Quartz, but I fear that I probably have to write my own layout routines. I'm hoping someone can save me some time.

STW
  • 44,917
  • 17
  • 105
  • 161
Owen Hartnett
  • 5,925
  • 2
  • 19
  • 35

1 Answers1

2

There's a way to do the opposite of the method you mentioned you'd done before--instead of determining the largest text that can fit in a rect, you can determine the size of the rect that is needed to contain text at a certain font size:

[@"Your Text Here" sizeWithFont:[UIFont fontWithName:@"Arial" size:34]];

Seems like you could do the same algorithm you mentioned but in reverse, increasing the font size until it was the largest size that could fit in your rect without overflowing it.

It's entirely possible there's a better way to do this, but I don't see why this method wouldn't work :)

WendiKidd
  • 4,333
  • 4
  • 33
  • 50
  • You're close enough I can credit you with the answer. Actually, you can use either approach (get smaller or get larger), and you want to use the sizeWithFont: constrainToSize: lineBreakMode: so that you can have multiple lines in the rectangle. – Owen Hartnett May 21 '12 at 14:36
  • Glad I could point you in the right direction! Hope your project goes well, and thanks for marking this as answer :) – WendiKidd May 21 '12 at 22:17