我有一个非常长的字符串,有数千行默认字体。所以,与其在一个表视图单元格中画出整件事,我还要创建几个单元格来绘制相同的字符串,每个单元格都绘制字符串的下一部分。
我很难找到一个起点。比如说,我在正方形中画出了字符串的前500个像素--我怎么知道从哪里开始呢?如果是相同的字符串,我如何指定它只绘制字符串的某些部分?
每个单元格都知道它是自己的行号,所以我可以确定我在表中的确切位置,我只是不知道字符串如何知道它应该画哪个部分。
或者另一个问题是:如何根据一定数量的行将一个字符串拆分成多个字符串?
编辑:这里有一些我发现可能有用的NSString方法,但我仍然不知道如何在我的例子中使用它们:
- (void)getLineStart:(NSUInteger *)startIndex end:(NSUInteger *)lineEndIndex contentsEnd:(NSUInteger *)contentsEndIndex forRange:(NSRange)aRange
- (NSRange)lineRangeForRange:(NSRange)aRange发布于 2012-05-10 16:04:09
使用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提供了一个范围,从字符串的0到一半。最后一行"\n“可能在那个范围内。然后我抓住了那个子串
https://stackoverflow.com/questions/10537477
复制相似问题