SwiftUI Representable TextField 在消失时更改字体
SwiftUI Representable TextField changes Font on disappear
我在 SwiftUI for MacOS 中使用 Representable ViewController 作为自定义 NSTextField
。我正在为该 TextField 应用自定义的增大字体大小。效果很好,我在上一个问题 .
中有一个关于该主题的问题
我现在正在 viewDidAppear()
方法中设置 TextField 的字体,效果很好。我可以看到更大的字体。现在的问题是,当我的视图没有聚焦时,文本大小正在缩小到正常水平。当我返回并再次激活 window 时,我看到了小字体。当我再次输入时,它会刷新并且我会看到正确的字体大小。
这是我正在使用的代码:
class AddTextFieldController: NSViewController {
@Binding var text: String
let textField = NSTextField()
var isFirstResponder : Bool = true
init(text: Binding<String>, isFirstResponder : Bool = true) {
self._text = text
textField.font = NSFont.userFont(ofSize: 16.5)
super.init(nibName: nil, bundle: nil)
NSLog("init")
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func loadView() {
textField.delegate = self
//textField.font = NSFont.userFont(ofSize: 16.5)
self.view = textField
}
override func viewDidAppear() {
self.view.window?.makeFirstResponder(self.view)
textField.font = NSFont.userFont(ofSize: 16.5)
}
}
extension AddTextFieldController: NSTextFieldDelegate {
func controlTextDidChange(_ obj: Notification) {
if let textField = obj.object as? NSTextField {
self.text = textField.stringValue
}
textField.font = NSFont.userFont(ofSize: 16.5)
}
}
这是代表:
struct AddTextFieldRepresentable: NSViewControllerRepresentable {
@Binding var text: String
func makeNSViewController(
context: NSViewControllerRepresentableContext<AddTextFieldRepresentable>
) -> AddTextFieldController {
return AddTextFieldController(text: $text)
}
func updateNSViewController(
_ nsViewController: AddTextFieldController,
context: NSViewControllerRepresentableContext<AddTextFieldRepresentable>
) {
}
}
这是一个演示:
我考虑过在视图返回时使用 Notification,但不确定
NotificationCenter.default.addObserver(self, selector: #selector(willEnterForeground), name: NSApplication.willBecomeActiveNotification, object: nil)
这是一个解决方案。使用 Xcode 11.4 / macOS 10.15.4
测试
class AddTextFieldController: NSViewController {
@Binding var text: String
let textField = MyTextField() // << overridden field
和单元格和控件所需的自定义 类(两者!!)(在所有其他地方,只需删除不再需要的用户字体使用):
class MyTextFieldCell: NSTextFieldCell {
override var font: NSFont? {
get {
return super.font
}
set {
// system tries to reset font to default several
// times (here!), so don't allow that
super.font = NSFont.userFont(ofSize: 16.5)
}
}
}
class MyTextField: NSTextField {
override class var cellClass: AnyClass? {
get { MyTextFieldCell.self }
set {}
}
override var font: NSFont? {
get {
return super.font
}
set {
// system tries to reset font to default several
// times (and here!!), so don't allow that
super.font = NSFont.userFont(ofSize: 16.5)
}
}
}
我在 SwiftUI for MacOS 中使用 Representable ViewController 作为自定义 NSTextField
。我正在为该 TextField 应用自定义的增大字体大小。效果很好,我在上一个问题
我现在正在 viewDidAppear()
方法中设置 TextField 的字体,效果很好。我可以看到更大的字体。现在的问题是,当我的视图没有聚焦时,文本大小正在缩小到正常水平。当我返回并再次激活 window 时,我看到了小字体。当我再次输入时,它会刷新并且我会看到正确的字体大小。
这是我正在使用的代码:
class AddTextFieldController: NSViewController {
@Binding var text: String
let textField = NSTextField()
var isFirstResponder : Bool = true
init(text: Binding<String>, isFirstResponder : Bool = true) {
self._text = text
textField.font = NSFont.userFont(ofSize: 16.5)
super.init(nibName: nil, bundle: nil)
NSLog("init")
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func loadView() {
textField.delegate = self
//textField.font = NSFont.userFont(ofSize: 16.5)
self.view = textField
}
override func viewDidAppear() {
self.view.window?.makeFirstResponder(self.view)
textField.font = NSFont.userFont(ofSize: 16.5)
}
}
extension AddTextFieldController: NSTextFieldDelegate {
func controlTextDidChange(_ obj: Notification) {
if let textField = obj.object as? NSTextField {
self.text = textField.stringValue
}
textField.font = NSFont.userFont(ofSize: 16.5)
}
}
这是代表:
struct AddTextFieldRepresentable: NSViewControllerRepresentable {
@Binding var text: String
func makeNSViewController(
context: NSViewControllerRepresentableContext<AddTextFieldRepresentable>
) -> AddTextFieldController {
return AddTextFieldController(text: $text)
}
func updateNSViewController(
_ nsViewController: AddTextFieldController,
context: NSViewControllerRepresentableContext<AddTextFieldRepresentable>
) {
}
}
这是一个演示:
我考虑过在视图返回时使用 Notification,但不确定
NotificationCenter.default.addObserver(self, selector: #selector(willEnterForeground), name: NSApplication.willBecomeActiveNotification, object: nil)
这是一个解决方案。使用 Xcode 11.4 / macOS 10.15.4
测试class AddTextFieldController: NSViewController {
@Binding var text: String
let textField = MyTextField() // << overridden field
和单元格和控件所需的自定义 类(两者!!)(在所有其他地方,只需删除不再需要的用户字体使用):
class MyTextFieldCell: NSTextFieldCell {
override var font: NSFont? {
get {
return super.font
}
set {
// system tries to reset font to default several
// times (here!), so don't allow that
super.font = NSFont.userFont(ofSize: 16.5)
}
}
}
class MyTextField: NSTextField {
override class var cellClass: AnyClass? {
get { MyTextFieldCell.self }
set {}
}
override var font: NSFont? {
get {
return super.font
}
set {
// system tries to reset font to default several
// times (and here!!), so don't allow that
super.font = NSFont.userFont(ofSize: 16.5)
}
}
}