How to determine which part of an nsstring fits into a rectangle?

It's about formatting PDFs using basic graphics. But it can be about any type of swap when it comes to printing a line on two pages.

I need to split a string into several pages. For small lines this is not a problem. For them, I use the NSString UIKit sizeWithFont sizeWithFont to determine if the full text on the current page is suitable or not. If so, I print it using drawInRect , and if not, move it to the next page.

Works fine, but not suitable for longer lines. In my application, individual lines (user-provided) may even be longer than a full page.

When there is a full page or some remaining space of the specified page, how can I determine exactly what part of the NSString can be displayed in the specified rectangle so that I can turn it off and print the remaining line (or at least part of it) on the next page?

I thought of an alternative. This brings the full text into the context of the graphic screen, and then cuts the created image into fragments that fit into the free space on the pages. But if I do this, I need to place the cut line exactly between the text lines. And to be honest, I'm not sure how to determine where the text containing the graphics can be safely cut into two (or more) parts without cutting the line of text in its middle.

I hope I expressed myself understandable and that someone is coming to the rescue.

+6
source share
3 answers

The main text has a very important function , the last argument of which (a pointer to CFRange) is defined as follows:

< fitRange > < fitRange >> fitRange dt> When you return contains a range of string that really matches the limited size. For>
+2
source

In the general case, when you are not using Core Text, you can simply add characters to the string until its size becomes too large. It can be pretty fast, and if you have really huge lines (as you think), you can speed it up with a simple binary search.

If you're using Core Text, just create a CTFrame for the area you want to fill, and then ask about the range that it used with CTFrameGetVisibleStringRange() .

+2
source

How about using approximation?

 - (NSInteger)lengthOfString:(NSString *)str thatFitsIntoView:(UIView *)displayView withFont:(UIFont *)font { CGSize textSize = [str sizeWithFont:font forWidth:displayView.frame.size.width lineBreakMode:NSLineBreakByWordWrapping]; NSInteger lengthThatFits; if (textSize.height > displayView.frame.size.height) { lengthThatFits = str.length * displayView.frame.size.height / textSize.height; } else { lengthThatFits = str.length; } return lengthThatFits; } 

Call it like this:

 NSString *str = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin eros" "nulla, commodo eget fringilla nec, varius nec ligula. Donec pulvinar eros sed diam" "euismod scelerisque. Mauris imperdiet leo at elit vulputate ullamcorper. Nullam" "condimentum odio a lacus tempus ac pretium justo tincidunt."; UIFont *font = [UIFont systemFontOfSize:16.0]; // or whatever UIView *displayView = // the view in which you intend to present the string NSInteger length = [self lengthOfString:str thatFitsIntoView:displayView usingFont:font]; NSString *displayedString = [str substringToIndex:length]; 
0
source

All Articles