2014-11-08 71 views
0

這是我的,我遇到的問題當前設置:問題,在關於空間的UITextView輸入和退格

  1. 有一個UITextView。用戶輸入他/她的搜索條件。
  2. 這些搜索項以字符串的形式分配。此 字符串將搜索條件合併到URL中。這將隨着用戶類型而更新。
  3. 該字符串顯示在用戶屏幕上的UILabel中。這將隨着用戶類型而更新。

這些都是我所面臨的問題:

  1. 當用戶退格的所有字符,第一個字符永遠不會消失的UILabel,直到新的字符類型。
  2. 我試圖設置限制來防止用戶輸入空格。但是,這些不起作用。
  3. 我試圖設置限制來防止用戶輸入多個空格。但是,該應用程序允許最多兩個空格。

這是我的代碼,用來解釋發生了什麼。

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; { 


    // unformatted verson of the original string (user input from the UITextView, 'keywords' is inserted into the URL 
    NSString *original = [NSString stringWithFormat:@"http://.com/?q=%@%@", self.keywords.text,string]; 

    // creates the formatted version of the string by replacing certain characters with others as lmgtfy was designed 

    NSString *formatted = original; 
       formatted = [formatted stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"]; 
       formatted = [formatted stringByReplacingOccurrencesOfString:@" " withString:@"+"]; 




    // displays the formatted version of the string, which is now actually a URL 
    self.link.text = formatted; 


    // removing whitespace at various locations to prevent misformatting of the string 
    // instance i: forbidding more than one space between search terms 
    // instance ii: forbidding a space to start the search terms 

    NSCharacterSet *whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet]; 
    if ([string rangeOfCharacterFromSet:whitespace].location != NSNotFound) 
    { 
     whitespaceCount++; 
     if (whitespaceCount > 1) 
     { 
      return NO; 
     } 
    } 
    else 
    { 
     whitespaceCount = 0; 
     return YES; 
    } 

    if (range.location == 0 && [string isEqualToString:@" "]) { 
     return NO; 
    } 
    return YES; 




    return YES; } 

對這三個問題的任何解決方案,將不勝感激。

+0

你使用的是UITextView還是UITextField?因爲代碼中的方法是UITextField的委託方法。那是什麼self.keywords?它是一個UITextView.And self.link是一個UILabel。 – 2014-11-08 14:07:35

+0

@NimishaPatel是的,你是正確的 – iPwnTech 2014-11-08 22:00:44

回答

1

我修改了代碼:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
{ 
    if(textField == self.keywords) 
    { 
     //prevents user to begin their input with space 
     if(textField.text.length == 0 && [string isEqualToString:@" "]) 
     { 
     return NO; 
     } 
     //Not allow the user to enter multiple space 
     char space = [textField.text characterAtIndex:textField.text.length-1]; 

    if([string isEqualToString:@" "] && (space == ' ')) 
    { 
     return NO; 
    } 

     NSMutableString *newString = [NSMutableString stringWithString:textField.text]; 

     if([string isEqualToString:@""]) 
     { 
     //If user press the backspace.Remove the last character. 
     [newString deleteCharactersInRange:NSMakeRange(newString.length-1, 1)]; 
     } 
    else 
    { 
     [newString appendString:string]; 
    } 

    // unformatted verson of the original string 
    NSString *original = [NSString stringWithFormat:@"http://.com/?q=%@",newString]; 

    NSString *formatted = original; 
    formatted = [formatted stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"]; 
    formatted = [formatted stringByReplacingOccurrencesOfString:@" " withString:@"+"]; 
    self.link.text = formatted; 
} 

return YES; 
} 

要轉換的字符串URL有很多方法的。 你也可以試試這個方法stringByAddingPercentEscapesUsingEncoding。

+0

良好的開端,但有一些要求已被這個實施帶走。首先,用戶只能夠在單詞之間輸入最多一個空格。其次,我需要一種特殊的URL編碼方式:將所有的''替換爲'+',所有'+'替換爲'%2B'(這些不會互相覆蓋) – iPwnTech 2014-11-10 08:41:15

+0

我已經提交了一個小的編輯,這使得你的代碼工作(更改爲'self.link.text = formatted;')。謝謝! – iPwnTech 2014-11-10 10:56:53

+0

感謝您的糾正。如果此代碼可以幫助您,那麼您可以接受答案或將其解決。:-) – 2014-11-10 11:44:28