Possible Duplicate:
UITextView : get text with wrap info
I have been scouting the NSString library and numerous libraries for a function that can take a long string like this :
Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui.
and together with a CGSize or float indicating the width, and the font being used, and return me a string with \n breaks and the words wrapped.
Result (roughly) :
Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac\n
egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet.\n
Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. \n
placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra.\n
Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi.\n
Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci,\n
sagittis tempus lacus enim ac dui.
I already know that UITextViews and such do this, but this is not helpful since I need to render the text in a raw OpenGL landscape, so I am using no regular UI elements.
I know this either exists as a framework, or a public class somewhere. I just simply can’t find any unified way of handling this.
I imagine it is close to [NSString sizeWithFont:forWidth:lineBreakMode:], but i do not need the size, I need the string itself.
There really is no need to reinvent this wheel, since it is exactly what the text engine does for you every time you wrap text. And what is the text engine? It is Core Text. If you drop down to the level of Core Text and have a CTFramesetter lay out the text for you, you can learn where it is putting the line breaks by asking for the resulting CTLines.
The documentation will get you started:
http://developer.apple.com/library/ios/#documentation/StringsTextFonts/Conceptual/CoreText_Programming/Operations/Operations.html
And there are lots of good tutorials on the Web.
Simple example:
As you will see, the output shows the range of each line of wrapped text. Isn’t this the sort of thing you’re after?