我正在Swift 5.0中进行编码。我想创建一个应用程序,用户可以在其中下载自定义字体并在任何应用程序中使用它们。我正在使用键盘扩展名来做到这一点。
这是我的代码
class KeyboardViewController: UIInputViewController {
override func viewDidLoad() {
super.viewDidLoad()
let button = createButton(title: "A")
self.view.addSubview(button)
}
func createButton(title: String) -> UIButton {
let button = UIButton(type: .system)
button.frame = CGRect(x: 0,y: 0,width: 20,height: 20)
button.setTitle(title,for: .normal)
button.sizeToFit()
button.translatesAutoresizingMaskintoConstraints = false
button.titleLabel?.font = UIFont(name: "Montague.ttf",size: 15)
button.backgroundColor = UIColor(white: 1.0,alpha: 1.0)
button.setTitleColor(UIColor.darkGray,for: .normal)
button.addTarget(self,action: #selector(didTapButton(sender:)),for: .touchUpInside)
return button
}
@objc func didTapButton(sender: AnyObject) {
let button = sender as! UIButton
button.titleLabel?.font = UIFont(name: "Montague.ttf",size: 15)
let title = button.title(for: .normal)
textDocumentProxy.insertText(title!)
}
这是输出。result
所以我的问题是如何使键盘按钮以我想要的字体显示和输出?