我正在使用iOS应用。在那个应用程序中我有文本域和文本视图。如果用户点击空格按钮两次,即使文本的开头或文本的结尾,我的要求是解除键盘。我尝试使用以下代码
** - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
NSLog(@"textView.text %@",textView.text);
NSUInteger newLength = (textView.text.length - range.length) + text.length;
//for white space starting
NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:@" "] invertedSet];
NSString *filtered = [[text componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
if([text isEqualToString:@"\n"]) {
[sendMsgTextView resignFirstResponder];
// [self animateTextView:textView up:NO];
return NO;
}
if ([textView.text isEqualToString:filtered]) {
[sendMsgTextView resignFirstResponder];
return NO;
}
返回YES; } **
但问题在于它仅限制文本的开头。如果我多次输入文本和点击空间,则键盘不会解除或不限制。任何人都可以了解这个问题。如果是这样,请提出宝贵的建议。
答案 0 :(得分:1)
请使用以下解决方案..
<。>文件中的
@property(nonatomic,retain)NSString * spaceStr;
<。>文件中的
** - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if([text isEqualToString:@"\n"]) {
[self.txtView1 resignFirstResponder];
// [self animateTextView:textView up:NO];
return NO;
}
if ([[self.spaceStr stringByAppendingString:text] isEqualToString:@" "]) {
self.spaceStr=@"";
[self.txtView1 resignFirstResponder];
return NO;
}
self.spaceStr=text;
return YES;
}
答案 1 :(得分:0)
我使用以下链接使用以下代码
- (BOOL) textView:(UITextView*)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text
{
//for restricting the double space for before entering text as well as after entering the text
if ( (range.location > 0 && [text length] > 0 &&
[[NSCharacterSet whitespaceCharacterSet] characterIsMember:[text characterAtIndex:0]] &&
[[NSCharacterSet whitespaceCharacterSet] characterIsMember:[[textView text] characterAtIndex:range.location - 1]]) )
{
//Manually replace the space with your own space, programmatically
textView.text = [textView.text stringByReplacingCharactersInRange:range withString:@""];
//Make sure you update the text caret to reflect the programmatic change to the text view
textView.selectedRange = NSMakeRange(range.location+1, 0);
//Tell Cocoa not to insert its space, because you've just inserted your own
return NO;
}
}
,链接是 iPhone: Disable the "double-tap spacebar for ." shortcut?