Clear button on UITextView - ios

How can I add a clear button (cross inside a circle) for UITextView like UITextField has?

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:

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

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.

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.

For me changing the .frame or the .contentInset properties did not work.
For me the best result came from:
1) adding a UIView to the controller, give it round corners and a border to mimic a UITextView.
self.viewTextBackground.layer.borderColor = [UIColor colorWithRed:171/255.0 green:171/255.0 blue:171/255.0 alpha:1.0].CGColor;
self.viewTextBackground.layer.borderWidth = 1.0f;
self.viewTextBackground.layer.cornerRadius = 9.0f;
2) place UITextView on top of this UIView. Place it so that the borders of the underlying UIView stay visible.
3) give the UITextView round corners:
self.textNote.layer.cornerRadius = 9.0f;
3) Make its width fe. 30pixels less compared to the underlying UIView. You now have space for a clear-button.
4) simply add a UIButton to your controller and place it in the top-right corner of the underlying UIView.
5) change the buttons properties: set its type to 'custom' and set its image to the image of a grey cross.
6) bind an action to the button te clear the UITextView

You can add a clear button like the one in the attached screenshot with minimal coding. Just follow these steps:
Select the storyboard and drag a UIButton into your UITextView
Set the buttons constraints
Assign a title or a background image
Create the button's IBOutlet reference and the action (see onClearPressed(_:) below) for "Touch Up Inside" in the ViewController
Implement the textViewDidChange(_:) UITextViewDelegate delegate method, and make sure to set the button's isEnabled property based on the textfield content, e.g.:
func textViewDidChange(_ textView: UITextView) {
clearButton.isEnabled = !textView.text.isEmpty
}
Implement the onClearPressed(_:) action:
#IBAction func onClearPressed(_ sender: Any) {
textView.text = ""
clearButton.isEnabled = false
}
That's it.

Related

Automatically disable UIButton with type custom when a UIAlertController covers the screen

I have a UIViewController containing multiple UIButton's of type custom. In cases where my view looses focus like a new view controller gets presented or some other view is added on top of it, how do I gray out(disable) my custom buttons automatically?
Basically I want to gray out the buttons which cannot receive touch and so the user focus is only on the interactive elements on the screen.
I am not using a button type with system because I want to set non-monochrome images for button image.
When UIAlertController shows up this changes tint color of the window. That is why non custom buttons are dimmed. These normal and dimmed colors are taken from tintColor property. In case of a custom button you can override tintColorDidChange method:
class CustomButton: UIButton {
override func tintColorDidChange() {
if tintAdjustmentMode == .dimmed {
// modify subviews to look disabled
} else {
// modify subviews to look enabled
}
}
}
But in your case it looks like your image has just one color. And that is why you can make it simpler. Just load button's image with rendering mode UIImageRenderingMode.alwaysTemplate and change button's tintColor be equal to the image's color. Then everything will happen automatically.
Making image be rendered in template mode is pssible with a help of Xcode
or get it programmatically originalImage.withRenderingMode(.alwaysTemplate) from original image.
You can use following code.
NSArray *subviews = [self.view subviews];
for (UIView *v in subviews) {
if ([v isKindOfClass:[UIButton class]])
{
UIButton *button = (UIButton *) v;
[button setEnabled:NO];
[button setAlpha:0.5];
}
}
This code will disable all the UIButtons in current view. You can re-enable the buttons when Alertview is dismissed.
Hope this helps!!
If you want button as untouchable You can use
button.enabled = false ;
button.userInteractionEnabled = false
before presenting your UIAlertViewController and
button.enabled = true ;
button.userInteractionEnabled = true
after presenting UIAlertViewController
and also if you want to change the color of button you can use
[btn setBackgroundImage:[UIImage imageNamed:#"your gray image"] forState:UIControlStateSelected];
or
[btn setBackgroundImage:[UIImage imageNamed:#"your gray image"] forState:UIControlStateHighlighted];
according to your choice

Show Clear Button of UITextField even it is empty

I have a UITextField for search in my Application.
When user taps on the Clear Button in the right of the Search Text Field I need to hide this textfield.
So, can I show the Clear Button then TextField is empty? I need to do it always to provide the ability hiding TextField.
Just create a Custom button and set it as rightView for your TextField
UIButton *cleanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
// Set clear image for your button
[cleanBtn setImage:img forState:UIControlStateNormal];
[cleanBtn addTarget:self action:#selector(cleanTextField:) forControlEvents:UIControlEventTouchUpInside];
yourTextField.rightViewMode = UITextFieldViewModeAlways;
yourTextField.rightView = cleanBtn;
I tried to fix this issue with creating a custom button and setting the rightView of my text field. It did not solve my problem. There is no button on the right view.
I have added UIButton in Interface Builder using Auto Layout and placed it on my text field. It works. It's strange solution but it works.

Having a UIButton be tapped in a UICollectionViewCell

I have a UICollectionView with its cells all laid out.
I have this declared as a subview:
#property (nonatomic, strong) UIButton *aButton;
I then have that declared in each cell like so:
if (_aButton == nil)
{
_aButton = [UIButton buttonWithType:UIButtonTypeSystem];
}
// Add in all _aButton info here
[self.contentView addSubview:_aButton];
// Call to button pressed for button
[_aButton addTarget:self action:#selector(aButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
The button click method is like so:
- (IBAction) aButtonPressed:(UIButton *) sender
{
// Code never gets heree
}
The if(_aButton== `nil) is needed since cells get reused.
How do I make this work now? Thanks.
Add button action code before button added to view.... May be it will work .
// Call to button pressed for button
[_aButton addTarget:self action:#selector(aButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
// Add in all _aButton info here
[self.contentView addSubview:_aButton];
I think, the way you are initializing the button is going to give you frame as 0,0,0,0 . So first give it a proper frame and title to be visible on the screen.
Then try tapping on that title and see if it works or not.
It's unclear what you trying to achieve here, but the button is not working because you not setting it's position and size on the cell. It could be done by setting frame or layout constraints.
Try to set button frame:
_aButton.frame = CGRectMake(0.0f, 0.0f, 50.0f, 50.0f);
You can also set background color for the button, just to make it visible on the cell:
_aButton.backgroundColor = [UIColor redColor];
Everything else is correct, button should work.

UIButton center position doesn't update with rotation

I have a program that dynamically generates UIButtons in the center of
the screen with push of another button.
The buttons are not updating the x-coordinates when I rotate the device.
Here is my code for creating buttons:
- (IBAction)createButton:(UIButton *)sender {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundColor:[UIColor redColor]];
[button addTarget:self action:#selector(doSomething:) forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake(self.xCoord,self.yOffset,100.0,120.0);
[self.view addSubview:button];
_yOffset = _yOffset+130;
}
The XCoord and self.yOffset were set in viewDidLoad:
- (void)viewDidLoad {
[super viewDidLoad];
self.yOffset = 109;
self.xCoord = self.view.bounds.size.width/2-50;
}
The coordinates of the button in your code are fixed once they've been added to the view. If you want to update the frame of the buttons when the screen rotates, you'll need to trigger the repositioning of the buttons when the screen rotates.
AutoLayout in a storyboard makes this easier (if you just want a button to appear then you can add it in a storyboard and change its hidden property from NO to YES when the other button is pressed -- this won't be helpful if you want to add an arbitrary number of buttons, although you could have a hidden placeholder view that you use as a positioning reference)
If you want to keep the buttons x-position, you need to add Auto Layout constraints. There are a couple ways to do this programmatically. You can use the NSLayoutConstraint object directly or use Auto Layout's Visual Format Language. For more info, see Apple's documentation.

inputAccessoryView on UIView

It's easy as pie to add an inputAccessoryView on a UITextField, UITextView, or UISearchBar. However, there's no obvious and easy way to add one for your basic UIView as far as I can tell!
I have a UIView subclass that follows the UIKeyInput protocol. It receives keyboard input to do stuff that isn't related to entering text, so I'd rather not force it to subclass the former specified views because it adds bloat and would expose a bunch of properties that don't do anything, plus I'd need to work around the text entry that occurs natively to those classes (more bloat).
However, my UIView does need an input accessory view on its presented keyboard to function correctly.
Are there any simple ways to go about this? Do I have to register as an observer to the UIKeyboardWillShowNotification in my UIView subclass and add a subview, as an accessory view, to it manually?
Did you try simply adding the inputAccessoryView method to your viewController?
I believe it gets called when the keyboard is shown, so you don't actually have to assign one to each textField or view.
- (UIView *)inputAccessoryView
{
if (!inputAccessoryView)
{
CGRect accessFrame = CGRectMake(0.0, 0.0, 768.0, 77.0);
inputAccessoryView = [[UIView alloc] initWithFrame:accessFrame];
inputAccessoryView.backgroundColor = [UIColor blueColor];
UIButton *compButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
compButton.frame = CGRectMake(313.0, 20.0, 158.0, 37.0);
[compButton setTitle: #"Word Completions" forState:UIControlStateNormal];
[compButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[compButton addTarget:self action:#selector(completeCurrentWord:) forControlEvents:UIControlEventTouchUpInside];
[inputAccessoryView addSubview:compButton];
}
return inputAccessoryView;
}

Resources