user1725650
user1725650

Reputation: 7

How can i search for text in a UITextView and delete the selected text from the view

New to Objective c so example would be appreciated. What I'm trying to do is input text into a UITextfield and remove the occurrence of the text in a UITextView on IBAction.

All knowing in html,javascript and css.Hard to teach a old dog new tricks,but working on it.
Thanks in advance to all that reply.

-(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];

}

Upvotes: 0

Views: 720

Answers (3)

NSTJ
NSTJ

Reputation: 3888

0) in your view controller:

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) then add:

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

to your view controller's viewDidLoad; method

2) add the following method to your view controller class

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

3) You may wish to do some funky regex work on the match for currentTextInField isEqualToString: though that is a separate question

Upvotes: 0

Ezeki
Ezeki

Reputation: 1519

this is how you can do it

NSString* searchWord = @"word";

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

textView.text = editedText;

Upvotes: 1

Sal Aldana
Sal Aldana

Reputation: 1235

Without seeing much of what you are trying to do I think you could just use the replaceOccuranceOfString method with NSStriing (you can look it up but it's something like NSString *newString = [oldString replaceOccuranceOfString:"what you want to replace" withString""];. But again it depends on what you are trying to accomplish in that IBAction, such as do you have a specific word that you want to remove or a list of words.

Upvotes: 1

Related Questions