Changing background color and font style of context menu

Viewed 27

I am using context menu to display a message for highlighted text. Pressing on the menu item won't do anything since I just wanted to display a value. However I want to change the background color of the context menu and font style of "Copied" text.

Below are my functions:

func tableView(_ tableView: UITableView, contextMenuConfigurationForRowAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
    return UIContextMenuConfiguration(identifier: indexPath as NSIndexPath, previewProvider: nil) { _ in
        return UIMenu(title: "", children: [
            UIAction(title: "Copied", image: UIImage(named: "copy"), attributes: .disabled) { action in
                // Do nothing. This is to just show a message.
            }
        ])
    }
}

func tableView(_ tableView: UITableView, previewForHighlightingContextMenuWithConfiguration configuration: UIContextMenuConfiguration) -> UITargetedPreview? {
    return makeTargetedPreview(for: configuration)
}

func tableView(_ tableView: UITableView, previewForDismissingContextMenuWithConfiguration configuration: UIContextMenuConfiguration) -> UITargetedPreview? {
    return makeTargetedPreview(for: configuration)
}

private func makeTargetedPreview(for configuration: UIContextMenuConfiguration) -> UITargetedPreview? {
    guard let indexPath = configuration.identifier as? IndexPath else { return nil }
    // Get the cell for the index of the model
    guard let cell = tableView.cellForRow(at: indexPath) as? CustomTableViewCell else { return nil }
    // Set parameters to a circular mask and clear background
    let parameters = UIPreviewParameters()
    parameters.backgroundColor = .clear
    parameters.visiblePath = UIBezierPath(roundedRect: cell.labelValue.bounds, cornerRadius: 5.0)

    // Return a targeted preview using our cell previewView and parameters
    return UITargetedPreview(view: cell.labelValue, parameters: parameters)
}

To change the font style, in "contextMenuConfigurationForRowAt", I tried adding let menu = UIMenu() menu.font = UIFont() // Couldn't find an API for font on UIMenu or on UIAction.

How do I change the font and background color of context menu?

enter image description here

0 Answers
Related