I'm trying to allow the user to select a font of his/her choice using the NSFontManager. I could show the font picker using the following code
@IBAction func fontchangeclicked(_ sender: Any) {
NSFontManager.shared.orderFrontFontPanel(nil)
}
But the NSViewController is unable to recognise this overridden function.
override func changefont(_ sender: Any?) {
guard let fontManager = sender as? NSFontManager else {
return
}
let newFont = fontManager.convert(self.globalfont)
self.globalfont = newFont
}
CodePudding user response:
A list of requirements:
- It's
changeFont
instead ofchangefont
. NSObject.changeFont(_:)
is deprecated, useNSFontChanging.changeFont(_:)
instead.- The view controller must adopt
NSFontChanging
. - Your
changeFont(_:)
is not called when the first responder object handleschangeFont(_:)
.
class ViewController: NSViewController, NSFontChanging {
func changeFont(_ sender: NSFontManager?) {
guard let fontManager = sender else {
return
}
let newFont = fontManager.convert(self.globalfont)
self.globalfont = newFont
}
}