我想让textField只接受小字母而非大写字母或没有任何特殊符号或不接受任何数字。
答案 0 :(得分:3)
您可以使用正则表达式,例如下面只允许使用小符号的正则表达式
{{1}}
答案 1 :(得分:1)
限制大写字母,数字和&特殊符号,在文本视图委托方法false
中以小写字母返回textField:shouldChangeCharactersInRange:
。
Swift 3.0:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
var myCharSet = CharacterSet(charactersInString: "abcdefghijklmnopqrstuvwxyz")
for i in 0..<(string.characters.count ?? 0) {
var c: unichar = string[i]
if !myCharSet.characterIsMember(c) {
return false
}
}
return true
}
答案 2 :(得分:1)
检查此更新的代码:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSLog(@"textField:shouldChangeCharactersInRange:replacementString:");
NSCharacterSet *myCharSet = [NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyz"];
for (int i = 0; i < [string length]; i++)
{
unichar c = [string characterAtIndex:i];
if (![myCharSet characterIsMember:c])
{
return NO;
}
}
return YES;
}
}
在Swift中
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
let set = NSCharacterSet(charactersInString: "abcdefghijklmnopqrstuvwxyz").invertedSet
return string.rangeOfCharacterFromSet(set) == nil
}
答案 3 :(得分:1)
请尝试以下代码,仅自动获取小写字母。
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var textEntry: UITextField!
//MARK : - Life Cycle
override func viewDidLoad() {
super.viewDidLoad()
textEntry.addTarget(self, action: #selector(self.textFieldDidChange), for: .editingChanged)
}
//MARK: - Lower letter function
func textFieldDidChange(sender: UITextField) {
sender.text = sender.text?.lowercased()
print(textEntry.text ?? "") //Check your text output here
}
}
答案 4 :(得分:1)
您也可以使用它,
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
// Return false when user tries to delete but textField is empty
let currentCharacterCount = textField.text?.characters.count ?? 0
if (range.length + range.location > currentCharacterCount) {
return false
}
// Return true when user tries to delete but textField is not empty
if string == "" {
return true
}
let userEnteredString = textField.text ?? ""
let newString = (userEnteredString as NSString).replacingCharacters(in: range, with: string) as NSString
return ((newString as String).range(of: "^[a-z]+$", options: .regularExpression) != nil)
}
答案 5 :(得分:0)
您必须使用自己的类继承UITextFieldDelegate类
class ViewController: UIViewController, UITextFieldDelegate {
添加IBOutlet
@IBOutlet weak var firstName: UITextField!
您必须确保该对象正在使用
override func viewDidLoad() {
super.viewDidLoad()
firstName.delegate = self
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if textField == firstName {
let allowedCharacters = "abcdefghijklmnopqrstuvwxyz"
let allowedCharacterSet = CharacterSet(charactersIn: allowedCharacters)
let typedCharacterSet = CharacterSet(charactersIn: string)
let alphabet = allowedCharacterSet.isSuperset(of: typedCharacterSet)
return alphabet
}
}