如何在UITextView中搜索文本并从视图中删除所选文本

时间:2012-10-12 15:40:12

标签: ios uitextfield uitextview

Objective c的新功能,我们将不胜感激。 我想要做的是将文本输入到UITextfield并删除IBAction上UITextView中文本的出现。

所有人都知道html,javascript和css.Hard教老狗新技巧,但正在研究它。
提前感谢所有回复。

-(IBAction)tel2{


[UIButton beginAnimations:nil context:nil];
[UIButton setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:tel2 cache:YES];
[UIButton setAnimationDelegate:self];
[UIButton setAnimationDidStopSelector:@selector(test)];
[UIButton setAnimationDuration:.5];

if(tails2.tag==11){
    [tel2 addSubview:tails2];
    tails2.tag=22;



    textField.text = [NSMutableString stringWithFormat:@" %@", textField2.text];
}
else{
    [tel2 addSubview:heads2];
    tails2.tag=11;


    textField.text = [NSMutableString stringWithFormat:@"%@ %@", textField.text, textField2.text];

    }

[UIView commitAnimations];

}

3 个答案:

答案 0 :(得分:1)

如果没有看到你想要做的很多事情,我认为你可以使用NSStriing的replaceOccuranceOfString方法(你可以查找它,但它类似于NSString *newString = [oldString replaceOccuranceOfString:"what you want to replace" withString""];。但这又取决于你想要做什么在IBAction中完成,例如你有一个你要删除的特定单词或一个单词列表。

答案 1 :(得分:1)

这就是你如何做到的

NSString* searchWord = @"word";

NSString* editedText = [textView.text stringByReplacingOccurrencesOfString:searchWord withString:@""];

textView.text = editedText;

答案 2 :(得分:0)

0)在视图控制器中:

UITextField *fooTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, textFieldWidth, textFieldHeight)];
[self.view addSubview:fooTextField];
[self.view bringSubviewToFront:fooTextField];
fooTextField.delegate = self; //you need to add a couple of delegate methods here also

1)然后添加:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidChange:) name:@"UITextFieldTextDidChangeNotification" object:fooTextField];

到视图控制器的viewDidLoad;方法

2)将以下方法添加到视图控制器类

   - (void)textFieldDidChange:(NSNotification *)notification {
        NSString *currentTextInTextField = [NSString stringWithString:[(UITextField*)notification.object text]];
        if ([currentTextInTextField isEqualToString:@"thisIsTheStringYouAreLookingFor"]) {
            [(UITextField*)notification.object setText:@"whateverYouWantToReplaceItWith"];
        }
    }

3)你可能希望对currentTextInField isEqualToString:的匹配做一些时髦的正则表达式工作,尽管这是一个单独的问题

相关问题