我正在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
所以我的问题是如何使键盘按钮以我想要的字体显示和输出?
您可以使用button的titleLabel属性来自定义按钮的标题字体。
button.titleLabel?.font = UIFont(name: "YourCustomFont", size: 10.0)
[当应用程序开发人员决定要在其应用程序AFAIK中使用哪种字体时,您无法在输出上更改字体。
编辑:嗯,我不知道您设置输出的含义,有关您链接到的应用程序的更多信息,请阅读以下SO文章:How to Insert NSAttributedString using custom keyboard extension?
tl; dr:您无法设置自定义字体,那些应用使用Unicode,并且如果您愿意,也应该这样做。