2011-10-20 109 views
0

我是新来的iPhone开发。需要帮助。我正在开发iPhone应用程序,其中包含UITextview,我想统计textview的单词不使用textview.text.length,只是字数。 我想TextView的自动滚动在一分钟内iOS UITextView自动滚动从上到下

-(IBAction)startbutton:(id)sender{ 
     timer = [NSTimer scheduledTimerWithTimeInterval:0.1 
               target:self 
               selector:@selector(updateScroll) 
               userInfo:nil 
               repeats:YES]; 
    self.startingTime = [NSDate date]; 
} 

-(void)updateScroll 
{ 
    double noSeconds = (double) [self.startingTime timeIntervalSinceNow] /60; 
    frtextview.contentOffset = CGPointMake(0,noSeconds * frtextview.contentSize.height); 
} 

上面的代码可以自动滚屏,但是TextView的滚动,从下往上,Iwant从上往下,我希望你能帮助我找到该代码有问题。谢谢你的帮助

回答

0

NSScanner可以用来做一个字数,扫描的空白和计数的事件应该工作。

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSScanner_Class/Reference/Reference.html

如果你简单地否定了你的contentOffset我认为这将扭转你的滚动的方向。

frtextview.contentOffset = CGPointMake(0, -(noSeconds * frtextview.contentSize.height)); 

这里有一个简单的例子:

-(unsigned) wordCount: (NSString *) textString 
{ 
NSScanner *wordScanner = [NSScanner scannerWithString: textString]; 
NSCharacterSet *whiteSpace = [NSCharacterSet whitespaceAndNewlineCharacterSet]; 

unsigned wordCount = 0; 
while ([wordScanner scanUpToCharactersFromSet: whiteSpace intoString: nil]) 
{ 
wordCount++; 
} 

return wordCount; 
} 

我原来的代码在这里那里有很多更详细的讨论:

http://hintsforums.macworld.com/archive/index.php/t-26871.html

+0

谢谢您只help.I知道NSScanner类在Mac OS X v10.0及更高版本中可用。我可以在iOS中使用它吗? –

+0

我已经在我的iOS应用中使用过它。 –

+0

你能告诉我如何使用NSScanner来计算单词吗?还是示例代码?谢谢你的帮助! –