我正在使用 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
所以我的问题是如何使键盘按钮外观并以我想要的字体输出?
最佳答案
您可以使用按钮的 titleLabel 属性自定义按钮的标题字体。
button.titleLabel?.font = UIFont(name: "YourCustomFont", size: 10.0)
您无法更改输出上的字体,因为应用程序开发人员决定他们要在应用程序中使用什么字体(据我所知)。
编辑:嗯,我不知道你设置输出的意思,有关你链接到的应用程序的更多信息,请阅读这篇文章: How to Insert NSAttributedString using custom keyboard extension?
tl;dr:您无法设置自定义字体,这些应用程序使用 unicode,如果您选择这样做,您也应该这样做。
关于ios - 如何将自定义字体实现到自定义键盘以在其他应用程序上使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58779706/