将键盘设置为文本字段仅限英语

时间:2016-04-23 17:31:47

标签: ios swift

有没有办法将键盘设置为仅限英文的文本字段?我不希望用户能够切换到另一种语言。

许多人认为/说这是不可能的,但是对于这个主题的回答有14票,请求不同。唯一的问题是,它是用客观的c写的,我只知道很快...... iPhone: Change Keyboard language programmatically

4 个答案:

答案 0 :(得分:4)

textField.keyboardType = .asciiCapable

应该这样做。

答案 1 :(得分:1)

我知道这是一个老问题,但这是:

$ args79 uname date pwd
/Users/jleffler/soq
Process pwd (PID 2105) exited with status 0 (0x0000)
Darwin
Sun Sep 15 09:04:37 MDT 2019
Process uname (PID 2103) exited with status 0 (0x0000)
Process date (PID 2104) exited with status 0 (0x0000)
$

这是一个func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { if !["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"].contains(string.lowercased()) { return false } return true } 方法,因此您需要使View Controller符合该方法。您还需要将文本字段的委托设置为self,就像这样:

UITextFieldDelegate

它的作用是检查用户键入的每个字母是否属于上面的字母列表...如果不是,则不允许文本字段更改。

答案 2 :(得分:0)

F iOS 10.0及更高版本:

a)Objective-C

<!DOCTYPE html>
<html>

<style>
  p {
    font-family: Calibri;
    font-size: 14px;
    font-weight: bolder;
    text-align: left;
  }

  p.fade {
    color: #CCCCCC;
    font-size: 14px;
  }

  em {
    font-style: italic;
    font-size: 16px;
    font-weight: lighter;
  }

  em.pass {
    font-style: italic;
    font-size: 16px;
    color: green;
  }

  em.fail {
    font-style: italic;
    font-size: 16px;
    color: red;
  }

  a {
    text-decoration: none;
  }

  a:hover {
    text-decoration: underline;
  }

  hr {
    align: left;
    margin-left: 0px;
    width: 500px;
    height: 1px;
  }

  table {
    border-collapse: collapse;
  }

  .main_head {
    background-color: orange;
    border: none;
    align: left;
    margin-left: 0px;
    width: 2000px;

  }

  tr {
    padding: 4px;
    text-align: center;
    border-right: 2px solid #FFFFFF;
  }

  tr[data-type="QA"] {
    color: #ff0000 !important;
  }

  tr:nth-child(even) {
    background-color: #f2f2f2
  }

  th {
    background-color: #cceeff;
    color: black;
    padding: 4px;
    border-right: 2px solid #FFFFFF;

  }


</style>

<body>
<table>
  <table>
    <tr data-type="QA">
      <td style='color:BlueViolet'>Details: TEST HTML</td>
    </tr>
  </table>
  <table>
    <th class="main_head" colspan=3> CPU: usr=0.89% sys=3.38% |ssssssssssssssssssssssssssssssss
      ------hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh---------------------------------------------------------------------------------------
    </th>
    <tr>
    </tr>
    <tr>
      <td>Name</td>
      <td>First Name</td>
      <td> Last Name</td>
      <td> Work</td>
      <td> Percentages</td>
    </tr>
  </table>
</table>
</body>
</html>

b)迅速

yourTextfield.keyboardType = UIKeyboardTypeASCIICapableNumberPad

答案 3 :(得分:0)

yourTextField.keyboardType = UIKeyboardType.alphabet

这是解决方案。 :)