如何快速设置文本字段的掩码?例如,当用户在 Objective-C 中输入类似电话格式时的电话文本字段:
self.textField.mask = @"(##)####-####";
Run Code Online (Sandbox Code Playgroud)
Swift 4 非常简单,并带有遮罩最大文本字段长度和处理后退空间
//MARK: - text field masking
internal func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
//MARK:- If Delete button click
let char = string.cString(using: String.Encoding.utf8)!
let isBackSpace = strcmp(char, "\\b")
if (isBackSpace == -92) && (textField.text?.count)! > 0 {
print("Backspace was pressed")
textField.text!.removeLast()
return false
}
if textField == txtworkphone
{
if (textField.text?.count)! == 3
{
textField.text = "(\(textField.text!)) " //There we are ading () and space two things
}
else if (textField.text?.count)! == 9
{
textField.text = "\(textField.text!)-" //there we are ading - in textfield
}
else if (textField.text?.count)! > 13
{
return false
}
}
}
Run Code Online (Sandbox Code Playgroud)
Swift 4&&5 非常简单的电话号码掩码
您必须将此代码用作文本字段委托。
func formattedNumber(number: String) -> String {
let cleanPhoneNumber = number.components(separatedBy: CharacterSet.decimalDigits.inverted).joined()
let mask = "## ### ###"
var result = ""
var index = cleanPhoneNumber.startIndex
for ch in mask! where index < cleanPhoneNumber.endIndex {
if ch == "#" {
result.append(cleanPhoneNumber[index])
index = cleanPhoneNumber.index(after: index)
} else {
result.append(ch)
}
}
return result
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard let text = textField.text else { return false }
let newString = (text as NSString).replacingCharacters(in: range, with: string)
textField.text = formattedNumber(number: newString)
return false
}
Run Code Online (Sandbox Code Playgroud)
这样您就可以在 Swift 中创建电话掩码。
首先为 String 类型创建一个漂亮的扩展。下标获取索引处的字符,该函数从 int! 获取 String.Index 类型。
extension String {
subscript (i: Int) -> String {
if countElements(self) > i {
return String(Array(self)[i])
}
return ""
}
func indexAt(theInt:Int)->String.Index {
return advance(self.startIndex, theInt)
}
}
Run Code Online (Sandbox Code Playgroud)
这是调用电话号码输入字段中的文本的函数:
func returnMaskedPhoneField(thePhoneText:String)->String{
var returnString = thePhoneText
//Trims non-numerical characters
returnString = returnString.stringByTrimmingCharactersInSet(NSCharacterSet.decimalDigitCharacterSet().invertedSet)
//Removes all spaces
returnString = returnString.stringByReplacingOccurrencesOfString(" ", withString: "")
//Checks if we need to format a mobile number
if thePhoneText[1] == "4"{
if countElements(returnString) > 7 {
returnString = returnString.stringByReplacingCharactersInRange(Range<String.Index>(start: returnString.indexAt(7), end: returnString.indexAt(7)), withString: " ")
}
if countElements(returnString) > 4 {
returnString = returnString.stringByReplacingCharactersInRange(Range<String.Index>(start: returnString.indexAt(4), end: returnString.indexAt(4)), withString: " ")
}
}else {
if countElements(returnString) > 6 {
returnString = returnString.stringByReplacingCharactersInRange(Range<String.Index>(start: returnString.indexAt(6), end: returnString.indexAt(6)), withString: " ")
}
if countElements(returnString) > 2 {
returnString = returnString.stringByReplacingCharactersInRange(Range<String.Index>(start: returnString.indexAt(2), end: returnString.indexAt(2)), withString: " ")
}
}
return returnString
}
Run Code Online (Sandbox Code Playgroud)
然后这里是你实现该函数的地方,将其放入你的viewDidLoad方法中:
aTextField.delegate = self
aTextField.addTarget(self, action: "validateTextFields:", forControlEvents: UIControlEvents.EditingChanged)
Run Code Online (Sandbox Code Playgroud)
班级中某个地方的这个人就是你的textfield代表:
func validateTextFields(sender:AnyObject){
if let textField = sender as? UITextField {
if textField == aTextField {
if let currentCurserPosition = aTextField?.selectedTextRange {
var isEndOfString = false
let currentCurserPositionInteger = textField.offsetFromPosition(textField.beginningOfDocument, toPosition: currentCurserPosition.start)
if currentCurserPositionInteger == count(textField.text){
isEndOfString = true
}
aTextField?.text = returnMaskedPhoneField(textField.text)
if isEndOfString == false {
aTextField?.selectedTextRange = currentCurserPosition
}
}else {
aTextField?.text = returnMaskedPhoneField(textField.text)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
它的工作原理如下:
学分:
http://pjeremymalouf.com/creating-a-text-mask-in-swift/
如果您想使用自定义,textField则可以使用 AKMaskField。
在哪里可以找到textField这样的电话:
| 归档时间: |
|
| 查看次数: |
24003 次 |
| 最近记录: |