I can think of nothing that's directly built in. So I'd suggest:
Split the string into components by [NSCharacterSet +whitespaceAndNewlineCharacterSet]
and [NSString -componentsSeparatedByCharactersInSet:]
. I considered recommending the higher-level NSLinguisticTagger
to get out whole words but that wouldn't allow for things like words with a colon on the end.
Of those words, find the typographically largest using the UIKit addition NSString -sizeWithAttributes:
(under iOS 7) or -sizeWithFont:
(under 6 or below). You're going to make the assumption that the largest will remain the largest as you scale the font size down, which I think will always be true because Apple doesn't do aggressive font hinting.
If that word is already less wide than your view's width, you're done. Just show the string.
Otherwise use a quick binary search, repeatedly querying the size, until you find the smaller font size that you need to within whatever precision you think is appropriate (0.1 of a point sounds reasonable to me but you get the point). Then show the whole string at that size.