0

我想在 UIMenuItem 的标题中添加 NSAttributedString 而不是 String,但我看不到任何可能的方法。甚至有可能子类 UIMenuItem 或其他东西来实现这一点?我在 iOS 上的 Telegram 应用程序上看到他们实现了类似的功能。 来自 Telegram 的带有 UIMenuItem 的图像

4

1 回答 1

0

这应该允许您使用 NSAttributedString 作为 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")
    }
}
于 2019-03-29T18:41:50.090 回答