在 UIKit 中,我可以像这样更改 UILabel 的字体大小以支持使用系统字体的动态类型:
UIFontMetrics.default.scaledFont(for: UIFont.systemFont(ofSize: 16))
Run Code Online (Sandbox Code Playgroud)
我错了还是没有办法在 SwiftUI 中做这样的事情?如果我想使用系统字体但也支持动态类型,我可以只使用默认字体样式 .title 等吗?
这个来自 HackingWithSwift 的解决方案似乎只适用于自定义字体,对吗?
在此先感谢您的任何建议!
我为 SwiftUI 创建了一个UIViewRepresentable包装UITextField,因此我可以在用户点击回车键时更改第一响应者。
这是我的 UIViewRepresentable (我删除了第一响应者代码以保持简单)
struct CustomUIKitTextField: UIViewRepresentable {
@Binding var text: String
var placeholder: String
func makeUIView(context: UIViewRepresentableContext<CustomUIKitTextField>) -> UITextField {
let textField = UITextField(frame: .zero)
textField.delegate = context.coordinator
textField.placeholder = placeholder
return textField
}
func updateUIView(_ uiView: UITextField, context: UIViewRepresentableContext<CustomUIKitTextField>) {
uiView.text = text
uiView.setContentHuggingPriority(.defaultHigh, for: .vertical)
uiView.setContentCompressionResistancePriority(.required, for: .vertical)
}
func makeCoordinator() -> CustomUIKitTextField.Coordinator {
Coordinator(parent: self)
}
class Coordinator: NSObject, UITextFieldDelegate {
var parent: CustomUIKitTextField
init(parent: CustomUIKitTextField) {
self.parent = parent …Run Code Online (Sandbox Code Playgroud)