【发布时间】:2019-11-10 02:22:44
【问题描述】:
我正在使用 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
所以我的问题是如何使键盘按钮外观并以我想要的字体输出?
【问题讨论】:
-
嘿,你能告诉我如何获得键盘的高度,UIResponder.keyboardWillShowNotification 的通知在键盘扩展中不起作用。有没有其他办法?
标签: ios swift custom-keyboard