An easy way to use `canPerformAction` for context menu in SwiftUI.

Overview

Use canPerformAction for TextField/Editor in SwiftUI

You know how a TextField displays a set of context menu?

It doesn't matter if it's UIKit or SwiftUI, and if it's a TextField (which uses UITextField) or TextEditor (which uses UITextView).

Now say I don't want to see all of those things, but (in my case) only cut/copy/paste.

Back in the days, in UIKit

All you had to do was to do

class MyUITextField: UITextField {
    override func textFieldCanPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        action == #selector(UIResponderStandardEditActions.copy(_:))
        || action == #selector(UIResponderStandardEditActions.cut(_:))
        || action == #selector(UIResponderStandardEditActions.paste(_:))
    }
}

Then, in my Code (or even for those crazy Storyboarders out there in my Storyboard) I'd use a MyUITextField instance instead of the default UITextField.

SwiftUI? UIViewRepresentable? Are you crazy...

Now, sure, my first tought was... Okay...

struct MyTextField: UIViewRepresentable {
(...)
    func makeUIView(context: Context) -> MyUITextField {
        // configure and return...
    }
(...)
}

But as you might imagine, it means you have to basically REIMPLEMENT the whole UITextField stuff! Not to mention, how do you translate Font to UIFont, and all kind of crazy things (e.g. specifying a frame, etc, ...)

Believe me, I did that, it wasn't nice to read by any means. Yurk.

Swizzling to the rescue!

So I was thinking, all it takes is a little bit of Swizzling. Look at App.swift now!

If you comment private let responder = Responder(), your Replace..., Look Up, Translate, Share... will be back! 😁

⚠️ WARNING

The fact that e.g. a SwiftUI TextField is implemented with a UITextField is obviously an implementation detail.

If Apple ever changes that (e.g. a Text isn't a UILabel), the trick won't work anymore, obviously.

You might also like...
A Slide Menu, written in Swift, inspired by Slide Menu Material Design
A Slide Menu, written in Swift, inspired by Slide Menu Material Design

Swift-Slide-Menu (Material Design Inspired) A Slide Menu, written in Swift 2, inspired by Navigation Drawer on Material Design (inspired by Google Mat

Slide-Menu - A Simple Slide Menu With Swift
Slide-Menu - A Simple Slide Menu With Swift

Slide Menu!! Весь интерфейс создан через код

Swift-sidebar-menu-example - Create amazing sidebar menu with animation using swift
Swift-sidebar-menu-example - Create amazing sidebar menu with animation using swift

 SWIFT SIDEBAR MENU EXAMPLE In this project I create a awesome side bar menu fo

Hamburger Menu Button -  A hamburger menu button with full customization
Hamburger Menu Button - A hamburger menu button with full customization

Hamburger Menu Button A hamburger menu button with full customization. Inspired by VinhLe's idea on the Dribble How to use it You can config the looks

📷A simple and convenient way to manage your webcam's picture settings, right from your menu bar
📷A simple and convenient way to manage your webcam's picture settings, right from your menu bar

Viewfinder A simple and convenient way to manage your webcam's picture settings, right from your menu bar. About • Download • Building from Source • C

An iOS drop down menu with pretty animation and easy to customize.
An iOS drop down menu with pretty animation and easy to customize.

IGLDropDownMenu An iOS drop down menu with pretty animation. Screenshot How To Use Use CocoaPods: pod 'IGLDropDownMenu' Manual Install: Just drap the

adb-tools-mac is a macOS menu bar app written in SwiftUI for common adb tools.
adb-tools-mac is a macOS menu bar app written in SwiftUI for common adb tools.

adb-tools-mac is a macOS menu bar app written in SwiftUI for common adb tools.

Ambar is a macOS Menu Bar app built with SwiftUI.
Ambar is a macOS Menu Bar app built with SwiftUI.

Ambar Ambar is a macOS Menu Bar app built with SwiftUI. It is a template project which means that it can be used as a starting point for a new Menu Ba

Easily create dragabble menu with SwiftUI
Easily create dragabble menu with SwiftUI

SwiftUIDragMenu Easily create dragabble menu The gifs are not very good quality. Content is fluid. Sorry for this :( How to install this package Open

Owner
StuFF mc
Started with iOS in 2008, with the Mac around 2010, with Swift in 2014. Previously started as a Professional Developer in 1999 and had my C64 in 1987 at age 11.
StuFF mc
Context menu similar to the one in the Pinterest iOS app

VLDContextSheet A clone of the Pinterest iOS app context menu. Example Usage VLDContextSheetItem *item1 = [[VLDContextSheetItem alloc] initWithTitle:

Vladimir Angelov 173 Mar 28, 2022
You can easily add awesome animated context menu to your app.

Context-Menu.iOS You can easily add awesome animated context menu to your app. Made in Check this [project on dribbble] (https://dribbble.com/shots/17

Yalantis 1.8k Nov 17, 2022
An iOS context menu UI inspired by Things 3.

Contextual menus with delightful animations and styles Total control over menu contents using your own UIViewControllers Tons of feature and interacti

GitHawk 971 Nov 30, 2022
RadialMenu is a custom control for providing a touch context menu (like iMessage recording in iOS 8) built with Swift & POP

RadialMenu Looking for help? For $150/hr I'll help with your RadialMenu problems including integrating it into your project. Email [email protected] t

Brad Jasper 297 Nov 27, 2022
SwiftySideMenu is a lightweight and easy to use side menu controller to add left menu and center view controllers with scale animation based on Pop framework.

SwiftySideMenu SwiftySideMenu is a lightweight, fully customizable, and easy to use controller to add left menu and center view controllers with scale

Hossam Ghareeb 84 Feb 4, 2022
EasyMenu - SwiftUI Menu but not only button (similar to the native Menu)

EasyMenu SwiftUI Menu but not only button (similar to the native Menu) You can c

null 10 Oct 7, 2022
Library provides easy to implement variation of Android (Material Design) Floating Action Button for iOS. You can use it as your app small side menu. 🌶

RHSideButtons ?? Library provides easy to implement variation of Android (Material Design) Floating Action Button for iOS. You can use it as your app

Robert Herdzik 166 Nov 14, 2022
JNDropDownMenu - Easy to use TableView style dropdown menu.

Overview Swift version of https://github.com/dopcn/DOPDropDownMenu Easy to use TableView style dropdown menu. Setup The only thing you

Javal Nanda 65 Jun 27, 2021
Control your display's brightness from the macOS menu bar. Simple and easy to use.

MonitorControl Lite Control your display's brightness from the macOS menu bar. Simple and easy to use. About MonitorControl Lite is a simplified versi

null 62 Dec 11, 2022
Easy-to-use action menu

About Navigate Features Installation Usage Delegate Features Highly customizable support dark/light theme corner radius blured background width (iPad)

Vlad Karlugin 4 Jan 6, 2023