0

I have a really long string, with thousands of line with a default font. So rather than draw the whole thing out in one table view cell, I'm going to make several cells to draw the same string, each drawing the next part of the string.

I'm having a hard time finding a starting point. Say I draw the first 500 pixels of height of the string in the rect - how do I know where to start in my second rect? If it's the same string, how can I specify for it to draw only some parts of the string?

Each cell will know it's own row number, so I'll be able to determine where exactly in the table I am, I just don't know how the string will know which part it's supposed to draw..

Or another question would be: how can I break up one string into multiple strings based on a certain number of lines?

Edit: Here are some NSString methods I'm finding that might be useful, but I still don't know how I would use them in my case:

- (void)getLineStart:(NSUInteger *)startIndex end:(NSUInteger *)lineEndIndex contentsEnd:(NSUInteger *)contentsEndIndex forRange:(NSRange)aRange
- (NSRange)lineRangeForRange:(NSRange)aRange

2 Answers 2

1

Use substringWithRange: This will allow you to select a start and end point of the string. I would grab each section by a number of characters. So section 1 would be 0-500 section 2 would be 500-1000. The problem here would be you may cut off in the middle of a sentence. You can use something like lineRangeForRange to determine your ranges for the substring.

lineRangeForRange
Returns the range of characters representing the line or lines containing a given range.

- (NSRange)lineRangeForRange:(NSRange)aRange
Parameters
aRange
A range within the receiver.
Return Value
The range of characters representing the line or lines containing aRange, including the line termination characters.

EDIT

NSString *string = @"tjykluytjghklukytgjhkkghkj sdkjlhfkjsadgfiulgeje fuaeyfkjasdgfueghf aksjgflkj. wyruehskjluishfoeifh uasyeajhkfa uiyelkjahsdf uayekljshdf aehkfjsd. \n I iheio;fajkdsf sdfhlueshkfjskdhf ujhelkjfh. luehljkfhlsdf. leufhlkjdshfa. \n euoiywhfldsjkhf euyhfsdlkj. ewhlkjfsd. euilhfsdkjishdkjf euhjklsfd. \n";
NSLog(@"string length:%i", [string length]);
NSRange range;
range.length = [string length]/2;
range.location = 0;
NSLog(@"LineRangeForRange:%i", [string lineRangeForRange:range].length);
NSLog(@"Substring:%@", [string substringWithRange:[string lineRangeForRange:range]]);

Log displays:

string length:295
LineRangeForRange:148
Substring:tjykluytjghklukytgjhkkghkj sdkjlhfkjsadgfiulgeje fuaeyfkjasdgfueghf aksjgflkj. wyruehskjluishfoeifh uasyeajhkfa uiyelkjahsdf uayekljshdf aehkfjsd.

So what this did was I supplied LineRangeForRange a range which was from zero to half of the string. It could the last end line "\n" with in that range. Then I grabbed that substring

6
  • Hmm for it to cut off in the middle of sentences would really ruin the experience..I wanted to have it as natural as possible..
    – Snowman
    May 10, 2012 at 16:22
  • lineRangeForRange I believe will return you with the a range that will not cut of your sentences. So for example you want range 0-500 characters, you use lineRangeForRange: with range 0-500 and it will return a range 0-450 (which would be the end of the line) and you use substringWithRange on 0-450.
    – Jaybit
    May 10, 2012 at 16:27
  • Wait so what does lineRangeForRange do exactly..I'm confused at the results it's giving me. If I give it 0, 250, it will return complete lines within that character range?
    – Snowman
    May 10, 2012 at 16:41
  • I have The apple is the pomaceous fruit of the apple tree, species Malus domestica in the rose family (Rosaceae). It is one of the most widely cultivated tree fruits, and the most widely known of the many members of genus Malus that are used by humans. Apples grow on small, deciduous trees. I'm giving it range of (0,1) in lineRangeForRange, but the substring returns me that entire paragraph. I don't understand what its doing?
    – Snowman
    May 10, 2012 at 16:45
  • I am guessing that if there is no end line character within the range it gives you the the next end line in the string. So (0,1) will give you the first paragraph.
    – Jaybit
    May 10, 2012 at 16:56
0

Doesn't sound like a way I would wanna read text in an app. Why not use a textView?

Edit: my recommendation wasn't to put a textView in a tableViewCell but to display the beginning of the text truncated and on click push a viewControllers that displays a textView. Kind of like Mail does.

5
  • Because I don't want to put a text view in a table cell..this is only for one section of text, I have other sections that don't need splitting, and I need all sections to have similar scrolling behavior. A text view has its own scrolling behavior, so mixed in with a table view will give a bad experience..
    – Snowman
    May 10, 2012 at 16:14
  • Pretty sure you can turn off scrolling in a text view.
    – jrturton
    May 10, 2012 at 16:40
  • Which would lead to a text view of height 50,000 px
    – Snowman
    May 10, 2012 at 16:40
  • What's the difference between that and 500 cells? You have one cell of appropriate height containing the text view. The text view doesn't scroll, but the table does.
    – jrturton
    May 10, 2012 at 16:42
  • You're not supposed to have table view cells higher than 2009 pixels. It's in the documentation
    – Snowman
    May 10, 2012 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.