How to disable interaction on NSTextAttachment? - ios

In a UITextView with editable = YES and selectable = NO, I want to disable interaction on NSTextAttachments. To be more specific, I don't want long presses on NSTextAttachments to highlight the attachment images. I want these long press gestures to be passed to UITextView as normal text selection gestures.
What I have tried:
textView:shouldInteractWithTextAttachment:inRange: can prevent the action sheet but can not prevent the temporary highlight of the attachment image nor can it make these long presses be handled as text selection gestures.

UITextView has several installed gesture recognisers. There is one that is a subclass of UITapGestureRecognizer which you can disable.

Related

How to make UIAlertController's message selectable

How to make UIAlertController's message selectable with a long press gesture recognizer?
Is there anyway to do this?
UIAlertController doesn't provide text selection functionality. If you want to do copy this then you could alternatively provide a button on your UIAlertController that copies a string to the pasteboard.
OR
Add a UITextField to UIAlertController, by this you can select the text in UITextField, to stop editing in it, implement delegate method for UITextField.

How to add long press functionality to a UITextField

I added a UILongPressGestureRecognizer to a UITextField only to find that the only thing that got triggered was the UITextField's magnifying glass; and then when the magnifying glass disappeared the context menu appeared.
What do I have to do in order for my UILongPressGestureRecognizer to be triggered instead of the default behavior?
I've already tried disabling all UILongPressGestureRecognizers before adding my own, as per some other SO answers, but to no avail.
Put a UIView on top of the textField with the same frame. Make it clear background. Add your gestures to your UIView and handle custom selections from there on. Make a tap gesture to start editing the text manually. And make a longpressgesture to do whatever you want. etc.

Custom badge issue in iOS

I have an view which is having a button and a label.
The label increments whenever the user adds items to the cart, which is done by using custom badge. But what I need to know is how to put onClick for that label. Button takes the onClick but label is not taking an action(UIGuesture).
I have attached the image where in clicking on the cart goes to another view, but clicking on label does not work.
Any help would be appreciated.
thanks.
Try enabling the user interaction on label.
label.userInteractionEnabled = YES;
By default user interaction is disabled on UILabel, which doesn't let any gesture to be recognized while applied on UILabel.
Better to use an open-source implementation:
MKNumberBadgeView
CustomBadge

why does GestureRecognizer not work on textview first time?

I have a textView to input some text or emotion. so I make another button to change the keyboard to custom emotion view, and I also use tap GestureRecognizer for when I want to change back to keyboard.
I found every time I need to touch the button twice, the tap GestureRecognizer can work well. I thought and search long time but no result, finally I fix it. I think some one will meet the same question, so I shared it below.
because my add tap gesture recognition 's code is in init code, but when I tap textview, keyboard was pop up, the textview's position was changed too, so the init tap gesture recognition code isn't work now.
I fix it with add tap gesture recognition delay 0.3 s.

How do I make the keyboard go away when the user clicks somewhere else? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Dismiss keyboard by touching background of UITableView
How do I make the keyboard go away when the user clicks somewhere else?
Note: I know how to make the keyboard disappear with sending the resignFirstResponder command to the UITextField. At present the "Done" button is connected to all the correct code to do this and this works.
I have a UITableView with different UITableViewCells, and if the user moves onto another cell I want the keyboard to disappear.
So what events do I also need to include the resignFirstResponder in, for the keyboard to disappear.
Suppose UITableViewCell A has the UITextField, and UITableViewCell B has a button. If the user presses the button in cell B, then I will need to send the command resignFirstResponder back to the UITextField in cell A. First of all the button has no idea which cell it should sent the command to, and second even if the button did know which cell to send the command to how would it?
There's no trivial way to do this. You can put a transparent set of "shield views" all the way around the text field that take up the rest of the screen, and use any touches on them to dismiss the keyboard.
You can create a generic 'hideKeyboard' method in which you can include all text fields that can be first responders. For example,
-(void) hideKeyboard {
[textFieldName resignFirstResponder];
[textFieldSurname resignFirstResponder];
for (UITextField * txtField in arrTextFields) {
[txtField resignFirstResponder];
}
}
Then, at various sections in your class, depending on the functionality required, call;
[self hideKeyBoard];
This simple method means you won't need to keep track of the individual item that 'has the focus' / first responder status.
How to touch any part of the screen to make the keyboard go away
To touch somewhere outside the UITableView and have the keyboard disappear, place an invisible button on top of the 'touch area' that you want to respond to. Then, simply call [self hideKeyboard] from the touch event for that invisible button. Using IB, drag a new rounded button onto your view, then size it to take up the full size of the screen. Next,drag the button up or down the controls list in the IB document window so that button is behind all text fields and buttons, but in front of anything else (like images etc.). Finally, change the type of the button to 'Custom' to make it invisible, but still respond to events. Now all you have to do is to connect the new button's 'touch up inside' event to trigger the 'hideKeyboard' method.
Additionally, see this post for a brilliant solution to dismiss the keyboard when the above solution doesn't work : stackoverflow question 1823317

Resources