仅从其中文本的末尾启用编辑 TextField
Enable editing TextField only from the end of the text inside it
我正在为 phone 号码创建一个 TextField。
但是我遇到了一些麻烦:我需要阻止用户从任何地方编辑它,而不是从字符串的结尾。
我尝试使用“Touch up Inside”重置插入符,但它似乎对 TextField 禁用。
有什么方法可以限制插入符号的可能位置吗?
问题是它破坏了我的代码,它格式化了 phone 数字。如果您有任何其他好的方法来格式化它而不会出现此问题(我需要字符串看起来完全像这样:+7 (123) 456-78-90,而不是任何其他方法),我将非常感谢您的帮助。
private static var prev_phone: String = ""
@IBAction func phoneFieldTextChanged(_ sender: Any){
if let phone = phone_field.text{
if(phone.count <= 4){
phone_field.text = "+7 ("
}
else{
if phone.count > RegistrationViewController.prev_phone.count{
if(phone.count == 7){
phone_field.text! += ") "
}
if(phone.count == 12){
phone_field.text! += "-"
}
if(phone.count == 15){
phone_field.text! += "-"
}
if(phone.count > 18){
phone_field.text = String(phone.prefix(18))
}
}
if phone.count < RegistrationViewController.prev_phone.count{
if(phone.count == 8){
phone_field.text! = String(phone.prefix(6))
}
if(phone.count == 12){
phone_field.text! = String(phone.prefix(11))
}
if(phone.count == 15){
phone_field.text! = String(phone.prefix(14))
}
if(phone.count > 18){
phone_field.text = String(phone.prefix(18))
}
}
}
}
else{
phone_field.text = "+7 ("
}
RegistrationViewController.prev_phone = phone_field.text!
}
在这种情况下,为什么要使用 UITextField ?
例如,您可以创建一个自己制作的自定义键盘,只有数字和删除按钮。
用户只能键入一个数字或将其删除。将键入的数字添加到格式化字符串由您决定。
将此 class 分配给您的文本字段以防止用户从任何地方对其进行编辑。
class RestrictionTextField: UITextField {
override func closestPosition(to point: CGPoint) -> UITextPosition? {
let beginning = self.beginningOfDocument
let end = self.position(from: beginning, offset: self.text?.count ?? 0)
return end
}
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if action == #selector(UIResponderStandardEditActions.copy(_:)) || action == #selector(UIResponderStandardEditActions.select(_:)) || action == #selector(UIResponderStandardEditActions.cut(_:)) || action == #selector(UIResponderStandardEditActions.paste(_:)) || action == #selector(UIResponderStandardEditActions.selectAll(_:)){
return false
}
else {
return super.canPerformAction(action, withSender: sender)
}
}
}
对于您的文本字段,您可以使用这个简单的解决方案。
let kPhonePattern = "+# (###) ###-##-##"
let kPhonePatternReplaceChar: Character = "#"
extension String {
var digits: String {
return String(filter(("0"..."9").contains))
}
func applyPatternOnNumbers(pattern: String = kPhonePattern, replacmentCharacter: Character = kPhonePatternReplaceChar) -> String {
var pureNumber = self.replacingOccurrences( of: "[^0-9]", with: "", options: .regularExpression)
for index in 0 ..< pattern.count {
guard index < pureNumber.count else { return pureNumber }
let stringIndex = String.Index(encodedOffset: index)
let patternCharacter = pattern[stringIndex]
guard patternCharacter != replacmentCharacter else { continue }
pureNumber.insert(patternCharacter, at: stringIndex)
}
return pureNumber
}
}
用法:
@IBAction func phoneFieldTextChanged(_ sender: UITextField){
sender.text = sender.text?.applyPatternOnNumbers()
}
我正在为 phone 号码创建一个 TextField。 但是我遇到了一些麻烦:我需要阻止用户从任何地方编辑它,而不是从字符串的结尾。
我尝试使用“Touch up Inside”重置插入符,但它似乎对 TextField 禁用。
有什么方法可以限制插入符号的可能位置吗?
问题是它破坏了我的代码,它格式化了 phone 数字。如果您有任何其他好的方法来格式化它而不会出现此问题(我需要字符串看起来完全像这样:+7 (123) 456-78-90,而不是任何其他方法),我将非常感谢您的帮助。
private static var prev_phone: String = ""
@IBAction func phoneFieldTextChanged(_ sender: Any){
if let phone = phone_field.text{
if(phone.count <= 4){
phone_field.text = "+7 ("
}
else{
if phone.count > RegistrationViewController.prev_phone.count{
if(phone.count == 7){
phone_field.text! += ") "
}
if(phone.count == 12){
phone_field.text! += "-"
}
if(phone.count == 15){
phone_field.text! += "-"
}
if(phone.count > 18){
phone_field.text = String(phone.prefix(18))
}
}
if phone.count < RegistrationViewController.prev_phone.count{
if(phone.count == 8){
phone_field.text! = String(phone.prefix(6))
}
if(phone.count == 12){
phone_field.text! = String(phone.prefix(11))
}
if(phone.count == 15){
phone_field.text! = String(phone.prefix(14))
}
if(phone.count > 18){
phone_field.text = String(phone.prefix(18))
}
}
}
}
else{
phone_field.text = "+7 ("
}
RegistrationViewController.prev_phone = phone_field.text!
}
在这种情况下,为什么要使用 UITextField ? 例如,您可以创建一个自己制作的自定义键盘,只有数字和删除按钮。 用户只能键入一个数字或将其删除。将键入的数字添加到格式化字符串由您决定。
将此 class 分配给您的文本字段以防止用户从任何地方对其进行编辑。
class RestrictionTextField: UITextField {
override func closestPosition(to point: CGPoint) -> UITextPosition? {
let beginning = self.beginningOfDocument
let end = self.position(from: beginning, offset: self.text?.count ?? 0)
return end
}
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if action == #selector(UIResponderStandardEditActions.copy(_:)) || action == #selector(UIResponderStandardEditActions.select(_:)) || action == #selector(UIResponderStandardEditActions.cut(_:)) || action == #selector(UIResponderStandardEditActions.paste(_:)) || action == #selector(UIResponderStandardEditActions.selectAll(_:)){
return false
}
else {
return super.canPerformAction(action, withSender: sender)
}
}
}
对于您的文本字段,您可以使用这个简单的解决方案。
let kPhonePattern = "+# (###) ###-##-##"
let kPhonePatternReplaceChar: Character = "#"
extension String {
var digits: String {
return String(filter(("0"..."9").contains))
}
func applyPatternOnNumbers(pattern: String = kPhonePattern, replacmentCharacter: Character = kPhonePatternReplaceChar) -> String {
var pureNumber = self.replacingOccurrences( of: "[^0-9]", with: "", options: .regularExpression)
for index in 0 ..< pattern.count {
guard index < pureNumber.count else { return pureNumber }
let stringIndex = String.Index(encodedOffset: index)
let patternCharacter = pattern[stringIndex]
guard patternCharacter != replacmentCharacter else { continue }
pureNumber.insert(patternCharacter, at: stringIndex)
}
return pureNumber
}
}
用法:
@IBAction func phoneFieldTextChanged(_ sender: UITextField){
sender.text = sender.text?.applyPatternOnNumbers()
}