Clear button on UITextView
Solution 1
just make a uibutton and put it on uitextview and set its action for clear text view;
uitextview.frame = (0,0,320,416);
uibutton.frame = (310,0,10,10);
[uibutton setimage:@"cross.png" forcontrolstate:uicontrolstatenoraml];
[uibutton addTarget:self action:@selector(clearButtonSelected:) forControlEvents:UIControlEventTouchUpInside];
-(void)clearButtonSelected{
uitextview=@"";
}
hope you want to clear the text view text when you click on cross button above is help if not understand then i can send you proper program for that
Solution 2
Based on the answer from GhostRider a more accurate and up to date implementation:
int kClearButtonWidth = 15;
int kClearButtonHeight = kClearButtonWidth;
//add the clear button
self.clearButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.clearButton setImage:[UIImage imageNamed:@"UITextFieldClearButton.png"] forState:UIControlStateNormal];
[self.clearButton setImage:[UIImage imageNamed:@"UITextFieldClearButtonPressed.png"] forState:UIControlStateHighlighted];
self.clearButton.frame = CGRectMake(0, 0, kClearButtonWidth, kClearButtonHeight);
self.clearButton.center = CGPointMake(self.textView.frame.size.width - kClearButtonWidth , kClearButtonHeight);
[self.clearButton addTarget:self action:@selector(clearTextView:) forControlEvents:UIControlEventTouchUpInside];
[self.textView addSubview:self.clearButton];
And the method
- (void)clearTextView:(id)sender{
self.textView.text = @"";
}
You can use this images for the two states of the button:
Solution 3
From product perspective, if you're going to have a clear button, you probably want to use a UITextField
instead of a UITextView
and UITextField
supports a clear button natively - set the clearButtonMode
property as such:
UITextField *textfield = ...;
textfield.clearButtonMode = UITextFieldViewModeAlways;
See screenshot:
You could use UITextFieldViewModeWhileEditing
to only present the clear button while the user is actively updating the content.
Solution 4
There's nothing built in like there is for the UITextField. You'd have to add the view yourself (probably a UIButton) and place it correctly and also somehow get the text to wrap around it correctly. (And I don't think the latter is really possible.)
Maybe instead you should display a toolbar above the keyboard (or an inputAccessoryView
if you're targeting 3.2 and later) that provides a clear button.
Abhinav
Updated on October 06, 2020Comments
-
Abhinav over 3 years
How can I add a clear button (cross inside a circle) for UITextView like UITextField has?
-
tom over 12 yearsrun into the same need, where to get this cross.png?
-
ninjudd over 9 yearsThanks! Exactly what I needed.
-
Mike Gledhill over 9 years(Sigh.) The question was how to add a Clear button to a UITextView (not a UITextField)... and that DOESN'T have a ClearButtonMode member function...!!
-
Zorayr over 9 years@Mike Gledhill, just updated the answer to address your comment, I think for something like clearing a
UITextView
you are better off with a clear button (i.e.UIButton
). I just don't see how an X clear button would work with multi-line text views. -
Mike Gledhill over 9 yearsA perfect answer, but with XCode 6's insistance on using AutoLayout, I had to give up on creating the button programmatically, as you must add Constraints to ensure it does appear in the right place. In the end, I added a UIButton directly on the Storyboard, and added constraints there instead. (Sigh...)