How to add more styling options to the UIMenuController of UITextView? - ios

When UITextView's allowsEditingTextAttributes property is enabled,
textView.allowsEditingTextAttributes = true
the textview can show the BIU (bold/italic/underlined) styling options in the context menu by UIMenuController.
UIMenuController - BIU Styling Options #1
UIMenuController - BIU Styling Options #2
I wonder how to add more styling options (e.g., strikethrough, highlight) to the context menu inside the BIU. For example, the iOS' native Notes app has four options (BIU + strikethrough) inside the styling menu.
BIU Styling Options in the native Notes app
Is there any way to do it? I have been spending hours to find a way to override the "Selector(("_showTextStyleOptions:"))" but couldn't find out how.. Please help me!!

When the editing menu is about to become visible, you get a canPerformAction(_:withSender:) call in your UITextView. This method is called again when the user selects a button within the menu. You can check if the font style button was selected and add a custom button to that submenu.
class MyTextView: UITextView {
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
let menuController = UIMenuController.shared
if var menuItems = menuController.menuItems,
(menuItems.map { $0.action }).elementsEqual([.toggleBoldface, .toggleItalics, .toggleUnderline]) {
// The font style menu is about to become visible
// Add a new menu item for strikethrough style
menuItems.append(UIMenuItem(title: "Strikethrough", action: .toggleStrikethrough))
menuController.menuItems = menuItems
}
return super.canPerformAction(action, withSender: sender)
}
#objc func toggleStrikethrough(_ sender: Any?) {
print("Strikethrough button was pressed")
}
}
fileprivate extension Selector {
static let toggleBoldface = #selector(MyTextView.toggleBoldface(_:))
static let toggleItalics = #selector(MyTextView.toggleItalics(_:))
static let toggleUnderline = #selector(MyTextView.toggleUnderline(_:))
static let toggleStrikethrough = #selector(MyTextView.toggleStrikethrough(_:))
}
According to documentation, you may have to call update() on the UIMenuController after adding the button. But this was not necessary in my case.

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)
}

how can I remove default menuItem in iOS16, including "Open Link" "Add to Reading list"

In a custom UITextView, I override this function:
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
return false
}
But it still shows two menuItems, "Open Link" and "Add to Reading list". I just want to show one "copy" item.
implement the method of UITextViewDelegate below this answer,retun an empty UIMenu that will disable all edit menu, you can try to only add the copy menu.
- (UIMenu *)textView:(UITextView *)textView editMenuForTextInRange:(NSRange)range suggestedActions:(NSArray<UIMenuElement *> *)suggestedActions API_AVAILABLE(ios(16.0)) {
return [UIMenu menuWithChildren:#[]];
}

How to Focus Accessibility On A Particular Segment in A UISegmentedControl

OK. This answer helps a lot. I can select an accessibility item when a screen is shown. I simply add
UIAccessibility.post(notification: .layoutChanged, argument: <a reference to the UI item to receive focus>)
to the end of my viewWillAppear() method, and the item receives focus.
However, in one of my screens, the item I want to receive focus is a UISegmentedControl, and, when focused, it always selects the first item, no matter which one is selected. Since I followed the excellent suggestion here, I have an accessibility label for each item in the control, and I'd like my focus to begin on whichever segment is selected.
Is there a way to do this? As a rule, I try to avoid "hacky" solutions (like the one I just referenced), but I'm willing to consider anything.
Thanks!
UPDATE: Just to add insult to injury, I am also having an issue with the item I want selected being selected, then a second later, the screen jumps the selection to the first item. That's probably a topic for a second question.
I created a blank project as follows to reproduce the problem:
The solution is taking the selectedIndex to display the selected segment and providing the appropriate segment object for the VoiceOver notification: easy, isn't it?
I naively thought that getting the subview in the segmented control subviews array with the selectedIndex would do the job but that's definitely not possible because the subviews can move inside this array as the following snapshot highlights (red framed first element for instance):
The only way to identify a unique segment is its frame, so I pick up the segmented control index and the frame of the selected segment to pass them to the previous view controller.
That will allow to display (index) and read out (frame that identifies the object for the notification) the appropriate selected segment when this screen will appear after the transition.
Hereafter the code snippets for the view controller that contains the 'Next Screen' button:
class SOFSegmentedControl: UIViewController, UpdateSegmentedIndexDelegate {
var segmentIndex = 0
var segmentFrame = CGRect.zero
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let segueName = segue.identifier {
if (segueName == "SegmentSegue") {
if let destVC = segue.destination as? SOFSegmentedControlBis {
destVC.delegate = self
destVC.segmentIndex = segmentIndex
destVC.segmentFrame = segmentFrame
}
}
}
}
#IBAction func buttonAction(_ sender: UIButton) { self.performSegue(withIdentifier: "SegmentSegue", sender: sender) }
func updateSegmentIndex(_ index: Int, withFrame frame: CGRect) {
segmentIndex = index
segmentFrame = frame
}
}
... and for the view controller that displays the segmented control:
protocol UpdateSegmentedIndexDelegate: class {
func updateSegmentIndex(_ index: Int, withFrame frame: CGRect)
}
class SOFSegmentedControlBis: UIViewController {
#IBOutlet weak var mySegmentedControl: UISegmentedControl!
var delegate: UpdateSegmentedIndexDelegate?
var segmentFrame = CGRect.zero
var segmentIndex = 0
var segmentFrames = [Int:CGRect]()
override func viewDidLoad() {
super.viewDidLoad()
mySegmentedControl.addTarget(self,
action: #selector(segmentedControlValueChanged(_:)),
for: .valueChanged)
mySegmentedControl.selectedSegmentIndex = segmentIndex
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
print(mySegmentedControl.subviews)
let sortedFrames = mySegmentedControl.subviews.sorted(by: { $0.frame.origin.x < $1.frame.origin.x})
for (index, segment) in sortedFrames.enumerated() { segmentFrames[index] = segment.frame }
if (self.segmentFrame == CGRect.zero) {
UIAccessibility.post(notification: .screenChanged,
argument: mySegmentedControl)
} else {
mySegmentedControl.subviews.forEach({
if ($0.frame == self.segmentFrame) {
UIAccessibility.post(notification: .screenChanged,
argument: $0)
}
})
}
}
#objc func segmentedControlValueChanged(_ notif: NSNotification) {
delegate?.updateSegmentIndex(mySegmentedControl.selectedSegmentIndex,
withFrame: segmentFrames[mySegmentedControl.selectedSegmentIndex]!) }
}
The final result is as follows:
Double tap to go to the next screen.
Select the next element to focus the second segment.
Double tap to select the focused element.
Get back to the previous screen thanks to the Z gesture natively known by iOS with the navigation controller. The delegate passes the index and the frame of the selected segment.
Double tap to go to the next screen.
The segment that was formerly selected is read out by VoiceOver and still selected.
You can now Focus Accessibility On A Particular Segment in A UISegmentedControl following this rationale.
I try to avoid "hacky" solutions (like the one I just referenced), but I'm willing to consider anything.
Unfortunately, this solution is a hacky one... sorry. However, it works and I couldn't find another one anywhere else: see it as a personal fix unless you get a cleaner one to share? ;o)
UPDATE... That's probably a topic for a second question.
I can't reproduce the behavior of your update: if you create a dedicated topic for this problem, please add the most detailed code and context so as to provide the most accurate solution.
i think this works~!
class VC {
let segment = UISegmentedControl()
func fucusSegment(index: Int) {
let item = segment.accessibilityElement(at: index )
UIAccessibility.post(notification: .layoutChanged, argument: item)
}
}

Programmatically Tap View

I have created a custom view that is to be used as a radio button with images and text. I need to be able to load the saved selection when the controller loads. I set my listeners this way:
for button in genderButtons {
button.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(genderTapped(_:))))
}
#objc private func genderTapped(_ sender: UITapGestureRecognizer) {
for button in genderButtons {
button.select(sender.view! == button) // Toggles the button to display selected/deslected state.
...
}
}
The problem is that I can't find a way to tell the view to select. I tried making the gesture recognizer and object, but it doesn't have any methods I can use to trigger it. The 'buttons' aren't actually buttons, they're views, so I can't send an action event.
How can I select the correct button with code?
Just call genderTapped directly, handing it the gesture recognizer already attached to the desired "button".
For example, if thisGenderButton is the one you want to "tap", say:
if let tap = thisGenderButton.gestureRecognizers?[0] as? UITapGestureRecognizer {
genderTapped(tap)
}
You can add this method in your customView like this,
Class CustomView: UIView {
public func select(_ value: Bool) {
self.backgroundColor = value ? .green: .red
}
}
and then in below method you can call select for the tapped view.
#objc private func genderTapped(_ sender: UITapGestureRecognizer) {
(sender.view as? CustomView)?.select(true)
}

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:"))
}
}

Resources