I know how to disable the standard copy, paste, cut... options in Swift when editing a UITextField's / UITextView's text but I don't know how to do this with a UISearchBar. With a text field I would subclass UITextField and override the canPerformAction() method. But when subclassing UISearchBar this does not work and I don't know how to address the searchBar's textField. Furthermore I would really like to know, how you could change the color of the little 'X' on the right site of the search bar's text field, which is responsible for deleting all the text in the text field.
Would be great if someone could help me with these two things. Thanks in advance.
Your best bet to change the clear button color is to use a custom image by calling
setImage(_ iconImage: UIImage?, forSearchBarIcon icon: UISearchBarIcon, state state: UIControlState)
on the UISearchBar.
As to disabling copy/paste on the UISearchBar's text field, I don't think there is a way to do that. Even if you get access to the UITextField instance contained in it, there's no way you'd be able to change the behaviour for that particular instance.
Related
I often want to put my own things in the same place the keyboard pops up, but for my own controls... such as putting a UIDatePicker there, or a custom UIPickerView, or whatever.
I came up with a clumsy way of getting this behavior by having a dummy UITextField and putting my custom view in its inputView property. Then when the user clicks on my item, I just trigger off the UITextField to display the view I've assigned to the inputView.
Then I got to wondering if there was a better less kludgey way to do this. I found this article Show UIPickerView like a keyboard, without UITextField where several people recommend the same thing I do.
My question is this. Is it common to (mis)use the UITextField in this manner?
Many times you will face a UITextfield that you would want to populate through a custom control other than the standrd keyboard. Thus the inputView method was declared and can be used.
What is recommended:
1- if the UItextfield is normal, use the keybard (don't change the input view)
2- if the value is numeric, show the numberpad in keyboard directly (textField.keyboardType = .numberPad)
3- if your textField is a date then you set the input view as a date picker.
4- sometimes you need a UITextField where you need to choose between stuff. Thus you develop your own custom UIPicker and set it as an input View.
5- If what you are tring to achieve don't fall in all the above then you can do your own inputView and assign it.
So in short don't be afraid, it is the normal thing to do!
Hope this helps!
Is it possible to add a done or cancel key which dismisses a keyboard to all keyboards in an iOS app? There are several posts asking how to dismiss a keyboard via a cancel or done button, but the solutions are on a field by field basis.
Are there any solutions that add this functionality globally so the code wont need to be duplicated for each textfield/area in an application?
Like #silentBob says in his answer, the inputAccessoryView of a text field is the view that’s displayed immediately above the keyboard when the text field is the first responder. If you didn’t want to write an extension on UITextField to override the -inputAccessoryView method, you could also create a subclass of UITextField to do the same, which would make it easier to determine which method is going to be called. You could also have multiple subclasses of UITextField to customize which button(s) appear. If you have a text field in a storyboard, you can simply change the class to your custom subclass, so while you have to go through and make those changes, you don’t have to do it in code.
Yes, you can add an extension to UITextField class, which should add a UIToolbar with Done and Cancel actions as UITextField inputAccessoryView.
Well, In that case you have to customise the keyboard, built your own keyboard and do whatever you need to do with your key in the keyboard.
Im trying to copy the content in a UILabel but without hiding the keyboard, the problem is that when I show the menu of copy in the label the label becomes first responder and the keyboard resign, but I want to do that without hiding the keyboard, is there a way to do that?
It is not possible with UILabel.
You should use UITextField for that. Just disable editing.
AFAIK, you can't do that. But I think you can have a work around for that. Instead of not hiding the keyboard, why don't keep track the current active text field and then active it after user press copy. You can use UIPasteboardChangedNotification to know when user pressed copy. For example:
self.lastActiveTextField = aTextField
-(void)pasteBoardDidChange:(NSNotification*)notif
{
[self.lastActiveTextField becomeFirstResponder];
}
I think so u r looking something like this project.
UILabel with UIKeyInput protocol implementation
https://github.com/hackiftekhar/IQEditableLabel
I think, it is not possible, there can be only one first responder at any time. If the keyboard is shown because of another UI element, then when you try to copy the content from UILabel, the OS has to transfer first responder from other element to UILabel, as there is no need of keyboard for UILabel, the keyboard will hide automatically. So, you have to make changes to your elements to fix this problem or use third party UI elements who can fix your problem.
Every UIView component has a method called: canBecomeFirstResponder. He is read only, but you can subclass the UI object and override the getter:
- (BOOL)canBecomeFirstResponder {
return false;
}
I didn't do the test, but if the "become first responder" is the problem, that should solve it.
I want to use a UITextField so that only part of it will be editable.
I know about the delegation and shouldChangeCharactersInRange but for some reason, copying the ranged part is allowed.
My goal is to get similar result to this (the 'subject' text part) without being able to copy it.
Should i use a different UITextField with textFieldDidBeginEditing returning false all the time?
Is there a better solution?
In the screenshot, a UILabel placed next to the UITextField is used. I'd recommend doing this as it will give you more options when you decide to style the text.
I have a non-editable UITextView to display some text. Users can select the text in this UITextView and choose the iOS "Speak Selection" functionality (speak button) to read it for them. However, when 'Speak' is done reading the last word, it scrolls up the UITextView. In fact, even if I select just the last word in the text, and choose 'Speak', it scrolls up the UITextView.
I have scrollEnabled set to NO, editable set to NO, and the text is a NSAttributedString.
How can I stop the UITextView to scroll up in this case?
I can't comment with my reputation, unfortunately I don't have a real answer but a workaround; so far mine is to intercept [UITextView setContentOffset:animated] via method swizzling (http://nshipster.com/method-swizzling/) and avoid calling the original method when needed (this method is called by QuickSpeak function). I guess subclassing would be cleaner if you can instantiate the view yourself (this is not my case).