這是我的,我遇到的問題當前設置:問題,在關於空間的UITextView輸入和退格
- 有一個
UITextView
。用戶輸入他/她的搜索條件。 - 這些搜索項以字符串的形式分配。此 字符串將搜索條件合併到URL中。這將隨着用戶類型而更新。
- 該字符串顯示在用戶屏幕上的
UILabel
中。這將隨着用戶類型而更新。
這些都是我所面臨的問題:
- 當用戶退格的所有字符,第一個字符永遠不會消失的
UILabel
,直到新的字符類型。 - 我試圖設置限制來防止用戶輸入空格。但是,這些不起作用。
- 我試圖設置限制來防止用戶輸入多個空格。但是,該應用程序允許最多兩個空格。
這是我的代碼,用來解釋發生了什麼。
- (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; }
對這三個問題的任何解決方案,將不勝感激。
你使用的是UITextView還是UITextField?因爲代碼中的方法是UITextField的委託方法。那是什麼self.keywords?它是一個UITextView.And self.link是一個UILabel。 – 2014-11-08 14:07:35
@NimishaPatel是的,你是正確的 – iPwnTech 2014-11-08 22:00:44