I'm trying to get a NSString broken into lines to fit a desired width (as it would happen inside sizeWithFont:constrainedToSize:
). Is there a better way to do this than guessing where the breaks would happen in sizeWithFont
?

- 1,492
- 16
- 21
-
Could you go into a bit more detail on why you need the separated lines? If you just want to draw the string in a given space then this is quite simple. – Ciarán Walsh Dec 28 '09 at 16:23
-
I'm creating a background - like the one you get when selecting text - for a label. It's width varies for each line (with different length), so I'll need them separately. – Pyetras Dec 28 '09 at 16:43
4 Answers
If NSLayoutManager
is indeed unavailable, you may be stuck with a more inefficient solution: starting with the first word of the string, appending each subsequent word and using the sizeWithFont:
(etc.) method on the resulting string. When the height of the returned CGSize
changes, the most recently appended word was the one that caused the line break, and you can thus split the string there.

- 57,021
- 16
- 130
- 131
Update: This apparently only applies on the Mac platform. Though NSLayoutManager is specifically referenced in the iPhone's String Drawing Guide (as of December, 2009), neither it nor NSAttributedString are available on the iPhone platform. Sorry for the noise.
Original Response
NSLayoutManager is your friend if you're looking to divide up the string itself into lines of a certain width, with a given set of attributes (ie, you need to know the actual places where the string is wrapped).
If you're only looking to wrap lines when drawing and you don't care where, NSAttributedString's (AppKit Additions) methods -drawInRect: or -drawWithRect:options: will do just fine.

- 60,946
- 14
- 140
- 135
-
1Neither NSLayoutManager nor NSAttributedString are available on the iPhone. – Ole Begemann Dec 27 '09 at 22:59
-
1I'm not going to draw the lines (immediately), so neither NSAttributedString nor NSString + UIKit additions are going to help. – Pyetras Dec 27 '09 at 23:33
-
Ole: Confusing. NSLayoutManager is specifically referenced in the String Programming Guide in the iPhone Dev Center's library: http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/Strings/Articles/DrawingStrings.html – Joshua Nozzi Dec 27 '09 at 23:52
Update: Based on the comment below (you can draw the string, but can't draw a background to cover the string), I think your best bet would be to use a UIWebView and use CSS to draw your background.
The NSString UIKit Additions might fit your needs, particularly
- (CGSize)drawInRect:(CGRect)rect withFont:(UIFont *)font
lineBreakMode:(UILineBreakMode)lineBreakMode;
and/or
- (CGSize)drawInRect:(CGRect)rect withFont:(UIFont *)font
lineBreakMode:(UILineBreakMode)lineBreakMode
alignment:(UITextAlignment)alignment;

- 36,329
- 7
- 58
- 54
-
Check out the comments on the question and Joshua's answer below—drawing the lines isn't the issue. He actually does need to know where the line breaks are, for the purpose of drawing a background that extends only to the end of each line. – Noah Witherspoon Dec 31 '09 at 06:05