我们如何显示UITEXTFIELD的建议Xcode Swift?
我在互联网上花了几个小时,但是我找不到适合自己的问题的解决方案。问题是遵循: 我已经实施了提交的文本,(借助stackoverflow
post)使用uiviewRepresentable
,它包装uitextfield
。我这样做是为了能够在ios15之前优雅地制作文本输入第一响应者。 我在许多地方和许多不同的页面上使用此文本字段。
当前,当用户开始在我们的自定义文本字段中键入文本时,我们需要以下拉列表的形式显示建议。将显示的视图应完全显示在每个文本字段的下方,并且不应破坏现有视图的流量和定位。当然,我们需要修改我们的文本字段实现,因为我们在许多不同的地方使用它看起来很难。
通常,如何实施这种行为?
波纹管我发布了我们的文本输入的实施
感谢
class TextFieldPad: UITextField {
let padding = UIEdgeInsets(top: 0, left: 15, bottom: 0, right: 15)
override open func textRect(forBounds bounds: CGRect) -> CGRect {
return bounds.inset(by: padding)
}
override open func placeholderRect(forBounds bounds: CGRect) -> CGRect {
return bounds.inset(by: padding)
}
override open func editingRect(forBounds bounds: CGRect) -> CGRect {
return bounds.inset(by: padding)
}
}
struct FocusableTextField: UIViewRepresentable {
class Coordinator: NSObject, UITextFieldDelegate {
@Binding var text: String
var didBecomeFirstResponder = false
init(text: Binding<String>) {
_text = text
}
func textFieldDidChangeSelection(_ textField: UITextField) {
text = textField.text ?? “”
}
}
@Binding var placeHolder: String;
@Binding var text: String
var isFirstResponder: Bool = false
var issecure: Bool = false
var borderColor: UIColor = UIColor(rgb: 0xEEA924);
var backgroundColor: UIColor = UIColor.white;
var placeholderColor: UIColor = UIColor.black;
var textColor: UIColor = UIColor.black;
func makeUIView(context: UIViewRepresentableContext<FocusableTextField>) -> UITextField {
let textField = TextFieldPad(frame: .zero)
textField.layer.borderWidth = 2;
textField.layer.borderColor = borderColor.cgColor
textField.layer.cornerRadius = CGFloat(14.0)
textField.layer.backgroundColor = backgroundColor.cgColor
textField.isSecureTextEntry = issecure
textField.textColor = textColor
textField.autocorrectionType = .no
textField.autocapitalizationType = .none
textField.attributedPlaceholder = NSAttributedString(
string: placeHolder,
attributes: [NSAttributedString.Key.foregroundColor : placeholderColor.cgColor]
)
textField.delegate = context.coordinator
return textField
}
func makeCoordinator() -> FocusableTextField.Coordinator {
return Coordinator(text: $text)
}
func updateUIView(_ uiView: UITextField, context: UIViewRepresentableContext<FocusableTextField>) {
uiView.text = text
if isFirstResponder && !context.coordinator.didBecomeFirstResponder {
uiView.becomeFirstResponder()
context.coordinator.didBecomeFirstResponder = true
}
}
}
I have spent several hours on internet, but I am unable to find suitable solution for my problem. The problem is following:
I have implemented text filed, (with the help of stackoverflow
post) using UIViewRepresentable
, which wraps UITextField
. I did this in order to be able to gracefully make text input first responder before IOs15.
I am using this text field in many places and many different pages.
Currently we have need to show suggestions in form of dropdown, when user starts typing text in our custom text field. The view that will be shown, should appear exactly below of the each text field and it should not disrupt the flow and positioning of existing views. Of course, we need to modify our text field implementation, because we are using it in so many different places different approach looks very hard.
In general how is it possible to implement this kind of behavior?
Bellow I am posting our implementation of text input
Thanks
class TextFieldPad: UITextField {
let padding = UIEdgeInsets(top: 0, left: 15, bottom: 0, right: 15)
override open func textRect(forBounds bounds: CGRect) -> CGRect {
return bounds.inset(by: padding)
}
override open func placeholderRect(forBounds bounds: CGRect) -> CGRect {
return bounds.inset(by: padding)
}
override open func editingRect(forBounds bounds: CGRect) -> CGRect {
return bounds.inset(by: padding)
}
}
struct FocusableTextField: UIViewRepresentable {
class Coordinator: NSObject, UITextFieldDelegate {
@Binding var text: String
var didBecomeFirstResponder = false
init(text: Binding<String>) {
_text = text
}
func textFieldDidChangeSelection(_ textField: UITextField) {
text = textField.text ?? “”
}
}
@Binding var placeHolder: String;
@Binding var text: String
var isFirstResponder: Bool = false
var issecure: Bool = false
var borderColor: UIColor = UIColor(rgb: 0xEEA924);
var backgroundColor: UIColor = UIColor.white;
var placeholderColor: UIColor = UIColor.black;
var textColor: UIColor = UIColor.black;
func makeUIView(context: UIViewRepresentableContext<FocusableTextField>) -> UITextField {
let textField = TextFieldPad(frame: .zero)
textField.layer.borderWidth = 2;
textField.layer.borderColor = borderColor.cgColor
textField.layer.cornerRadius = CGFloat(14.0)
textField.layer.backgroundColor = backgroundColor.cgColor
textField.isSecureTextEntry = issecure
textField.textColor = textColor
textField.autocorrectionType = .no
textField.autocapitalizationType = .none
textField.attributedPlaceholder = NSAttributedString(
string: placeHolder,
attributes: [NSAttributedString.Key.foregroundColor : placeholderColor.cgColor]
)
textField.delegate = context.coordinator
return textField
}
func makeCoordinator() -> FocusableTextField.Coordinator {
return Coordinator(text: $text)
}
func updateUIView(_ uiView: UITextField, context: UIViewRepresentableContext<FocusableTextField>) {
uiView.text = text
if isFirstResponder && !context.coordinator.didBecomeFirstResponder {
uiView.becomeFirstResponder()
context.coordinator.didBecomeFirstResponder = true
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最终使用
InputAccessoryView
,并在键盘上显示该视图中的可滚动建议。欢迎任何其他意见和建议:)
I ended up using
inputAccessoryView
and displaying scrollable suggestions inside that view over keyboard.Any alternative opinions and suggestions are welcome :)