【问题标题】:How to implement custom fonts to a custom keyboard to use on other apps?如何将自定义字体实现到自定义键盘以在其他应用程序上使用?
【发布时间】: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


【解决方案1】:

您可以使用按钮的 titleLabel 属性自定义按钮的标题字体。

button.titleLabel?.font = UIFont(name: "YourCustomFont", size: 10.0)

您无法更改输出字体,因为应用开发者决定他们想在他们的应用 AFAIK 中使用什么字体。

编辑:好吧,我不知道您设置输出是什么意思,有关您链接到的应用程序的更多信息,请阅读此 SO 帖子: How to Insert NSAttributedString using custom keyboard extension?

tl;dr:你不能设置自定义字体,那些应用程序使用 unicodes,如果你选择,你也应该这样做。

【讨论】:

猜你喜欢
  • 2012-09-30
  • 1970-01-01
  • 1970-01-01
  • 2012-10-02
  • 2015-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多