0

I would like to add NSAttributedString in UIMenuItem's title instead of String, but I don't see any possible way of doing this. Is it even possible to maybe subclass UIMenuItem or something else to achieve this? I saw that on Telegram application on iOS they achieved something like this. Image with UIMenuItem from Telegram.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • It can be custom component that made by telegram. MenuItem does not have customizable view or label. – bpolat Mar 29 '19 at 17:29
  • Custom Component you mean subclass of UIMenuItem or something else? Do you have any idea how they achieved this effect? – Johnny Lynch Mar 31 '19 at 21:41

1 Answers1

0

This should allow you to use NSAttributedString for the title of your UIMenuItem:

import UIKit

class CustomUIMenuItem: UIMenuItem {
    init(titleAttributedString: NSAttributedString, action: Selector) {
        super.init(title: titleAttributedString.string, action: action)
    }
}

class ViewController: UIViewController {

    @IBOutlet weak var textView: UITextView!

    override func viewDidLoad() {
        super.viewDidLoad()
        let item = CustomUIMenuItem(titleAttributedString: NSAttributedString(string: "Menu Title"), action: #selector(testFunction))
        UIMenuController.shared.menuItems = [item]
    }

    @objc func testFunction() {
        print("Success")
    }
}
Wilfried Josset
  • 1,096
  • 8
  • 11