gpt4 book ai didi

ios - SwiftUI 内的 UITextView View 背景和深色模式的边框颜色(表单)

转载 作者:行者123 更新时间:2023-12-01 23:38:24 33 4
gpt4 key购买 nike

我试图让 UITextView 看起来与(SwiftUI)TextField 相同,UITextView 代码如下:

...
func makeUIView(context: UIViewRepresentableContext<TextView>) -> UITextView {
let textView = UITextView()

textView.font = UIFont.preferredFont(forTextStyle: UIFont.TextStyle.body)
textView.text = placeholderText
textView.textColor = .placeholderText
textView.backgroundColor = .systemBackground
textView.layer.borderColor = UIColor.placeholderText.cgColor
textView.layer.borderWidth = 1
textView.layer.cornerRadius = 6

return textView
}
...

完整代码类似于this

在表单中,它在浅色模式下看起来不错: enter image description here

但是,在深色模式下的表单中:

enter image description here

我做错了什么:

textView.backgroundColor = .systemBackground
textView.layer.borderColor = UIColor.placeholderText.cgColor

最佳答案

啊,我明白了。我的 UITextView 位于 UIViewController 中(以更好地控制大小)

该设置不会出现灰色背景。

public class CustomTextView: UIViewController {
var textView: UITextView!

init() {
super.init(nibName: nil, bundle: nil)
self.textView = UITextView(frame: .zero)
self.textView.frame = self.view.bounds
self.view.addSubview(textView)
}
required init?(coder: NSCoder) {
super.init(coder: coder)
textView = UITextView(frame: .zero)
textView.frame = self.view.bounds
self.view.addSubview(textView)
}
}

然后在你的UIViewRepresentable

public func makeUIViewController(context: UIViewControllerRepresentableContext<TextViewController_UI>) -> TextViewController_UI.CustomTextView {
let textViewCont = CustomTextView()

textViewCont.textView.font = UIFont.preferredFont(forTextStyle: UIFont.TextStyle.body)
textViewCont.textView.text = "placeholder"
textViewCont.textView.textColor = .placeholderText
textViewCont.textView.backgroundColor = .systemBackground
textViewCont.textView.layer.borderColor = UIColor.placeholderText.cgColor
textViewCont.textView.layer.borderWidth = 1
textViewCont.textView.layer.cornerRadius = 6
return textViewCont
}

关于ios - SwiftUI 内的 UITextView View 背景和深色模式的边框颜色(表单),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60066633/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com