Custom UITextView Can Not Be Set in Identity Inspector - ios

I just want to override a method in UITextView which is canPerformAction. In the overriden method I try to disable "paste:" action. In order to do that I tried to create a new type which will only handles the action not to done. All other uitextview related codes in the default ViewController.swift. So I just added a cocoa touch class in the project and set the target. Here is my custom UITextView's code as looks:
import UIKit
public class CustomTextView: UITextView {
override public func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool {
if action == "paste:" {
return false
}
return super.canPerformAction(action, withSender: sender)
}
}
When I select one of the uitextviews in the storyboard and view the Identity Inspector pane, I see that I m not able to set my custom class.
So what am I missing?

I've just tried your code and It's working. Just write: CustomTextView in your class in main.storyboard.

Related

How to remove Search Web from UITextView menu

// Subclass UITextView to remove default context menu items like Learn and Share and keep Cut, Copy and Paste
class TextView : UITextView {
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
let actions = [#selector(copy(_:)), #selector(cut(_:)), #selector(paste(_:))]
return actions.contains(action)
}
}
I've used the above code to remove excessive menu items and only keep cut/copy/paste but recently there's a new menu item called Search Web that searches the highlighted text in Safari/Google. Is there a way to disable that too?
Remove it in buildMenu(with builder:)
override func buildMenu(with builder: UIMenuBuilder) {
if builder.menu(for: .lookup) != nil {
builder.remove(menu: .lookup)
}
super.buildMenu(with: builder)
}

Remove Bold/Italic/Underline in toolbar selection in WKWebView

Is it possible to remove the Bold/Italic/Underline selection when highlighting and selecting a text in WKWebView?
Select a word, then press "Select"
The following dialog will present. Notice the "Bold/Italic/Underline" section. How do I remove this?
You should subclass WKWebView and override canPerformAction(_:withSender:) in your subclass.
The selector that displays the bold/italic/underline menu item is called _showTextStyleOptions: and it's an Objective-C method. The double parentheses prevent the compiler from showing a warning that says the method is not accessible.
import WebKit
class CustomWebView: WKWebView {
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
return action != Selector(("_showTextStyleOptions:"))
}
}

Connect IBAction to custom class

I wrote a custom class called PressableView. It recognizes taps and then calls a protocol function on its delegate object. Since it is a subclass of UIView, it does not allow the connection of IBActions by default.
I was wondering whether it is possible to connect a function inside the view controller, the pressable view is in, to the object, so it calls that method on tap – just like you would with a UIButton.
I already tried things like:
class PressableView: UIView {
// ...
#IBOutlet var action: (() -> Void)?
// ...
}
...but, Xcode doesn't allow that type to be an IBOutlet.
Any ideas?
change PressableView parent class as UIControl class then you can connect for actions and handle it.
UIControl is subclass of UIView class only. so you will have all the properties of UIView as well.
class PressableView: UIControl {
#IBAction func uicontrolEventAction(_ sender: Any) {
}
}

Hide keyboard on tap on each view - Swift

I'm not going to ask how to hide the keyboard after you are done with editing a textField. My question is : Is there a way to do this on each view ? (like a setting) or do I need to write the two following functions and set the delegate properly every time ?
func textFieldShouldReturn(textField: UITextField) -> Bool // called when 'return' key pressed. return NO to ignore.
{
textField.resignFirstResponder()
return true;
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
}
I'm developing an app with a lot of textfields (and also views) so I try to avoid redundance code. What is the best solution to avoid this repetition?
Thank you!
You can create your own text field, which is subclass of UITextField. See the simple custom UITextField below:
import UIKit
class CustomTextField: UITextField, UITextFieldDelegate {
override func awakeFromNib() {
super.awakeFromNib()
self.delegate = self
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
self.resignFirstResponder()
return true
}
}
Use your custom text field name to Custom Class in your Storyboard.
See my example at Github
The easiest thing to do, is put one giant invisible button the size of the screen underneath your text fields, then when a non text field is tapped, you call the invisible button action to close it. If this does not apply in your scenario please let me know.
Create an IBAction method to dismiss keyboard
#IBAction func backgroundTapped (sender: UIView)
{
sender.endEditing(true)
}
Change the class of your UIView to UIControl which contains the textfields in storyboard (You can even do that to your view of the viewcontroller as shown)
It looks like this:
Now you can connect the IBAction to the Touch Up Inside event of this view, in the storyboard, as shown.

iOS Swift Remove Paste Option [duplicate]

This question already has answers here:
How to disable copy paste option from UITextField programmatically
(17 answers)
Closed 7 years ago.
I want to remove the option "Paste" from the selector
I've tried the code below but it adds other selections to the selector and the paste option is still there, it just disables the functionality
override func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool {
if action == "paste:" {
return false
}
return true
}
I want to remove Paste all together so the user doesn't even have the option to see it or click it
Have a look at UIMenuController
You should be able to use
var menuItems: [AnyObject]?
to set up your own object.
It is not a simple switch on or off the existing buttons, looks like you have to provide your own.
try this :
Step 1: You need to create another class which extends the UITextField. In this example, I made my CustomizedUITextField.
import UIKit
class CustomTextField: UITextField {
override func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool {
if action == "paste:" {
return false
}
return super.canPerformAction(action, withSender: sender)
}
}

Resources