自动填充 first/last 姓名

AutoFill first/last name

我最近设置了关联的域并添加了密码自动填充,但我现在想知道如何自动填充他们的名字和姓氏,如以下 HTML 中的某些网页所示:

<!DOCTYPE html>
<html>
<head>
<script>
function formSubmit() {
    document.forms["myForm"].submit();
}
</script>
</head>
<body>

<h1>The form name attribute</h1>

<form name="myForm" action="/action_page.php" method="get">
  <label for="fname">First name:</label>
  <input type="text" id="fname" name="fname"><br><br>
  <label for="lname">Last name:</label>
  <input type="text" id="lname" name="lname"><br><br>
  <input type="button" onclick="formSubmit()" value="Send form data!">
</form>

<p>Notice that the JavaScript in the head section uses the name of the form to specify which form to submit.</p>

</body>
</html>

如果您在 iPhone 上并点击上面的文本框,在某些情况下它会显示您的名字以便快速自动填充。现在我的问题是,如何使用 swiftUI 产生与上述相同的概念。

到目前为止,这是我在 SwiftUI 中的代码:

@State var displaynamefirst = ""


          TextField("First Name",text:self.$displaynamefirst)
            .autocapitalization(.words)
            .padding()
            .background(RoundedRectangle(cornerRadius:6).stroke(Color("Dominant"),lineWidth:2))
            .padding(.top, 0)
            .submitLabel(.done)

对于 TextField,您可以使用 .textContentType(_:) 修饰符。

documentation

One of the content types available in the UITextContentType structure that identify the semantic meaning expected for a text-entry area. These include support for email addresses, location names, URLs, and telephone numbers, to name just a few.

list of possible contentTypes


示例:

TextField("First Name",text:self.$displaynamefirst)
        .autocapitalization(.words)
        .padding()
        .textContentType(.givenName)          
        .background(RoundedRectangle(cornerRadius:6).stroke(Color("Dominant"),lineWidth:2))
        .padding(.top, 0)
        .submitLabel(.done)