0

I am coding in Swift 5.0. I want to create an app where users can download custom fonts and use them in any app. I am using a keyboard extension to do that.

Here is my code

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!)
}

Here is the output.result

So my question is how to make a keyboard button appearance and output in the font I want?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • hey can you tell me how to get height of keyboard , Notification for UIResponder.keyboardWillShowNotification is not working in keyboard extension . Is there any other way ? – Amit Verma Dec 29 '19 at 03:31

1 Answers1

0

You can customize your button's title font using the titleLabel attribute of button.

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

You can't change font on the output as app developers decide what fonts they want to use in their app AFAIK.

Edit: Well I didn't know what you meant by setting the output, for more info about the app you linked to please read this SO post: How to Insert NSAttributedString using custom keyboard extension?

tl;dr: You can't set a custom font, those apps use unicodes and if you choose to, you should too.

Don
  • 490
  • 3
  • 9
  • As you can see, that's what I did. However, I want the output to be in my custom font. There are a lot of apps in the App Store that have this feature. e.g., https://apps.apple.com/ru/app/fonts/id1454061614?l=en – Mikail Izaev Nov 09 '19 at 16:28