2011-10-20 82 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來計算單詞嗎?還是示例代碼?謝謝你的幫助! –