我为 UITextField 创建了一个 UIViewRespresentable 。我想应用修饰符:
myTextField.font(.headline).keyboardType(keyboardType)
即使我的 UIViewRepresentable 很简单,这也不起作用:
class MyTextField: UITextField { ... }
struct MyFieldField: UIViewRepresentable {
private let field = MyTextField()
func makeUIView(context: Context) -> MyTextField { return field }
func updateUIView(_ uiView: MyTextField, context: Context) {
...
}
}
UITextField
是一个 UIKit 组件,而不是 SwiftUI 组件。目前不尊重.font()
或任何其他 SwiftUI 修饰符。此外,你目前无法创建 https://developer.apple.com/documentation/swiftui/font a UIFont
从 SwiftUIFont
。所以,虽然你可以做@Environment(\.font) var font: Font
要获取当前值,您将无法在 UIKit 领域中使用它做任何有用的事情。
You can:
- 只需设置一个
UIFont
直接在你的UIViewRepresentable
- Use
UIFont.TextStyle
- 之间的地图
Font.TextStyle
and UIFont.TextStyle
对于其中任何一个,您都需要在您的UIViewRepresentable
保持该值,然后在期间应用它updateUIView(context:)
struct MyFieldField: UIViewRepresentable {
// Your three options
var myUIFont: UIFont
var myUITextStyle: UIFont.TextStyle
var mySwiftUITextStyle: Font.TextStyle // map this onto UIFont.TextStyle
...
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)