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
4

2 回答 2

1

使用substringWithRange:这将允许您选择字符串的起点和终点。我会用一些字符来抓取每个部分。因此,第 1 部分将是 0-500,第 2 部分将是 500-1000。这里的问题是你可能会在句子中间中断。您可以使用 lineRangeForRange 之类的东西来确定子字符串的范围。

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.

编辑

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]]);

日志显示:

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

所以这样做是我为 LineRangeForRange 提供了一个从零到字符串一半的范围。它可能是该范围内的最后一行“\ n”。然后我抓住了那个子字符串

于 2012-05-10T16:04:09.653 回答
0

听起来不像是我想在应用程序中阅读文本的方式。为什么不使用 textView?

编辑:我的建议不是将 textView 放在 tableViewCell 中,而是显示被截断的文本的开头,然后单击推送显示 textView 的 viewControllers。有点像Mail。

于 2012-05-10T16:04:56.710 回答