你也可以繼承的UITextField和創建自定義字段如下:
@IBDesignable
class LimitedLengthField: UITextField {
@IBInspectable var maxLength: Int = 16 // set maximum number of characters
var stringValue: String { return text ?? "" }
override func awakeFromNib() {
super.awakeFromNib()
keyboardType = .ASCIICapable // set the keyboard type
addTarget(self, action: #selector(editingChanged), forControlEvents: .EditingChanged)
editingChanged(self)
}
func editingChanged(sender: UITextField) {
sender.text = String(stringValue.characters.prefix(maxLength))
}
}
如果您需要廣告的一些自定義格式它會得到一點點,但棘手:
斯威夫特2
class CustomField: UITextField {
var stringValue: String { return text ?? "" }
var decimalDigits: String {
return stringValue
.componentsSeparatedByCharactersInSet(NSCharacterSet.decimalDigitCharacterSet().invertedSet)
.joinWithSeparator("") ?? ""
}
override func awakeFromNib() {
super.awakeFromNib()
addTarget(self, action: #selector(editindChanged(_:)), forControlEvents: .EditingChanged)
keyboardType = .NumberPad
editindChanged(self)
}
func editindChanged(sender: UITextField) {
let letters = Array(decimalDigits.stringByPaddingToLength(16, withString: " ", startingAtIndex: 0).characters)
switch decimalDigits.characters.count {
case 1...4:
text = decimalDigits
case 5...8:
text = (String(letters[0...3]) + "-"
+ String(letters[4...7]))
.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
case 9...12:
text = (String(letters[ 0...3 ]) + "-"
+ String(letters[ 4...7 ]) + "-"
+ String(letters[8...11]))
.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
case 13...16:
text = (String(letters[ 0...3 ]) + "-"
+ String(letters[ 4...7 ]) + "-"
+ String(letters[8...11]) + "-"
+ String(letters[12...15]))
.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
default:
print(text)
if decimalDigits.characters.count > 0 {
text = String(text!.characters.prefix(19))
}
}
}
}
斯威夫特3
class CustomField: UITextField {
var stringValue: String { return text ?? "" }
var decimalDigits: String {
return stringValue
.components(separatedBy: CharacterSet.decimalDigits.inverted)
.joined(separator: "") ?? ""
}
override func awakeFromNib() {
super.awakeFromNib()
addTarget(self, action: #selector(editindChanged(_:)), for: .editingChanged)
keyboardType = .numberPad
editindChanged(self)
}
func editindChanged(_ sender: UITextField) {
let letters = Array(decimalDigits.padding(toLength: 16, withPad: " ", startingAt: 0).characters)
switch decimalDigits.characters.count {
case 1...4:
text = decimalDigits
case 5...8:
text = (String(letters[0...3]) + "-"
+ String(letters[4...7]))
.trimmingCharacters(in: .whitespaces)
case 9...12:
text = (String(letters[ 0...3 ]) + "-"
+ String(letters[ 4...7 ]) + "-"
+ String(letters[8...11]))
.trimmingCharacters(in:. whitespaces)
case 13...16:
text = (String(letters[ 0...3 ]) + "-"
+ String(letters[ 4...7 ]) + "-"
+ String(letters[8...11]) + "-"
+ String(letters[12...15]))
.trimmingCharacters(in: .whitespaces)
default:
print(text)
if decimalDigits.characters.count > 0 {
text = String(text!.characters.prefix(19))
}
}
}
}
可能重複[設置UITextField的最大字符長度](http://stackoverflow.com/questions/433337/set-the-maximum-character-length-of-a-uitextfield) – ebby94
謝謝。我會嘗試。 :) –