2013-01-16 28 views
0

对于我编写的iOS应用程序,我使用的是UITextView,用户可以在其中插入有限的文本。UITextView用最大行长度和行换行

向该TextView的有2个限制:

  1. 线可以不超过30个字符
  2. 只能有20在的UITextView的文本行。

所以总之,最多是20行30个字符。

当用户键入的UITextView的和当前句子一些文本为30个字符,我希望它自动插入新行\n(在该行的最后一个字之前),并迫使硬道理,光标移到下面一行。

当用户有20行30个字符(甚至更简单的说:20行,最后一行有30个字符)时,我希望输入被阻止。

现在,这其中大部分都相当“简单”,但我所拥有的代码并未考虑边界情况,例如在前面的行中插入文本。

我环顾了Apple的文档,但我无法找到一种方法来实际强制在UITextView上包装这种Word。

我的尝试是在shouldChangeTextInRange委托方法中处理所有这些(使代码稍微冗长一点,所以它更容易阅读)。

#define MAX_LENGTH_LINE 30 
#define MAX_LENGTH_ROWS 20 

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text 
{ 
    // Check for backspaces, they should always be allowed? 
    if ([text length] == 0 && ![text isEqualToString:@"\n"]) 
     return YES; 

    NSArray* lines = [textView.text componentsSeparatedByString:@"\n"]; 

    // Check if there are a maximum of lines and the last line is already maxed out 
    NSString* lastLine = [lines objectAtIndex:[lines count] - 1]; 

    if (([lines count] == MAX_LENGTH_ROWS) && 
     (lastLine != nil) && 
     ([lastLine length] > MAX_LENGTH_LINE) && 
     ([text length] > 0)) 
     return NO; 


    if ((lastLine != nil) && 
     ([lastLine length] > MAX_LENGTH_LINE)) 
    { 
     NSRange range = [textView.text rangeOfString:@" " options:NSBackwardsSearch]; 
     NSRange breakRange = [textView.text rangeOfString:@"\n" options:NSBackwardsSearch]; 

     if (breakRange.location == NSNotFound) 
      breakRange = NSMakeRange(0, 1); 

     if (range.location == NSNotFound) { 
      range = NSMakeRange(0, 1); 
     } 

     if (range.location > breakRange.location) 
     { 
      textView.text = [textView.text stringByReplacingCharactersInRange:NSMakeRange(range.location, 1) withString:@"\n"]; 
     } 
     else 
     { 
      textView.text = [textView.text stringByAppendingString:@"\n"]; 
     } 
    } 

    if ([text isEqualToString:@"\n"]) 
    { 
     if ([lines count] == MAX_LENGTH_ROWS) 
      return NO; 
     else { 
      return YES; 
     } 
     NSRange range = NSMakeRange(textView.text.length - 1, 1); 
     [textView scrollRangeToVisible:range]; 
    } 

    return YES; 
} 

在此期间,我一直在这一段时间,我现在失去了它。任何人都可以提供一些指针,将UITextView限制为我想要的20行/ 30个字符的限制?

回答

0

这可能与您的总体目标相反,但我头脑中最简单的答案是在用户每次添加角色时重新分析字符串。那么添加角色的位置并不重要。而不是在shouldChangeTextInRange中做所有这些:在textViewDidChange:中执行。您需要成为UITextViewDelegate,并且您需要一个类NSString来保存上次成功的用户文本更新,以防您的用户尝试添加超出允许限制的字符。

事情是这样的:

-(void)textViewDidChange:(UITextView *)textView 
{ 
    //Get the textview without any new line characters 
    NSMutableString *temporaryString = [NSMutableString stringWithString:[textView.text stringByReplacingOccurrencesOfString:@"\n" withString:@" "]]; 
    bool updatePossible = true; 

    int i = 0, numberOfLinesSoFar = 0; 
    while(i + 30 < [temporaryString length]) 
    { 
     //Go 30 characters in and start the reverse search for a word separation 
     i += 30; 
     int j = i; 
     //Get the location of the final word separation in the current line 
     while(j >= i && [temporaryString characterAtIndex:j] != ' ') 
     { 
      j--; 
     } 

     //This means we found a word separation 
     if(j > i) 
     { 
      i = j; 
      [temporaryString replaceCharactersInRange:NSMakeRange(i,1) withString:@"\n"]; 
     } 
     //We didn't find a word separation 
     else 
     { 
      //Here we will just have to break the line at 30. 
      [temporaryString insertString:@"\n" atIndex:i]; 
     } 

     numberOfLinesSoFar++; 

     //Check if we just wrote line 20 and still have characters to go. 
     if(numberOfLinesSoFar > 19 && i < [temporaryString length]) 
     { 
      //Revert user change to the last successful character addition 
      textView.text = lastSuccessfulViewString; 
      updatePossible = false; 
      break; 
     } 
    } 

    if(updatePossible) 
    { 
     //If we are within the limits then update the global string (for undoing character additions) and the textview 
     textView.text = temporaryString; 
     lastSuccessfulViewString = temporaryString; 
    } 
} 

现在,这不会让用户把自己的换行字符,但可能有一对夫妇如果再语句来处理。

+0

的事情是,用户应该能够自己的新行添加到文本,使问题变得有点差。 –