2017-08-07 23 views
1

我在我的iOS應用程序中使用Swift語言開發的登錄頁面。 在第一個字母不應該與單/雙空間和用戶輸入文本後,它應該允許單個空間,不允許雙空間,並需要設置文本長度的限制。如何在Swift 3中限制TextField的雙/單空格iOS

我想限制用戶在敲擊單層/雙層空間,同時啓動時間輸入文本,並需要允許單個空格輸入第一個字母文本框後

我能夠做的單/雙的一方,但不按照我的要求工作。

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { 
    //for double space restrict 
    if (range.location > 0 && (textField.text?.characters.count)! > 0) { 
     //Manually replace the space with your own space, programmatically 
     //Make sure you update the text caret to reflect the programmatic change to the text view 
     //Tell Cocoa not to insert its space, because you've just inserted your own 
     return false 
    }else { 
     if textField.tag == 1 || textField.tag == 2 { //restrict text length 
      guard let text = textField.text else { return true } 
      let newLength = text.characters.count + string.characters.count - range.length 
      return newLength <= limitLengthForTextField 
     } 
    } 

    return true 
} 

任何人都可以提出建議。謝謝

+0

你可以添加你現在使用的代碼嗎? –

+0

只需檢查天氣textfield.text最後一個字符是否爲空格。 – soul

回答

0

請檢查這個代碼。

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { 

    let trimmedString = textField.text?.trimmingCharacters(in: .whitespaces) 
    if (trimmedString.length == 0) 
     return false 

    //for double space restrict 
    if (range.location > 0 && (textField.text?.characters.count)! > 0) { 
     //Manually replace the space with your own space, programmatically 
     //Make sure you update the text caret to reflect the programmatic change to the text view 
     //Tell Cocoa not to insert its space, because you've just inserted your own 
     if string.range(of:" ") != nil { 
      return false 
     } 
     return true 
    }else { 
     if textField.tag == 1 || textField.tag == 2 { //restrict text length 
      guard let text = textField.text else { return true } 
      let newLength = text.characters.count + string.characters.count - range.length 
      return newLength <= limitLengthForTextField 
     } 
    } 

    return true 
} 

和你的限制textfield length代碼應該可以正常工作,因爲它是textfields與標籤1 & 2.

+0

我有Objective-C的想法,我正在尋找斯威夫特,我不想修剪,我應該限制用戶,同時按下文本框中的單/雙空格輸入文本時間。 –

+0

只需更新我的代碼即可。 –

+0

我想,你不明白我的問題,我想限制用戶在編輯文本框時點擊單/雙空間。 Ur代碼將用於修剪文本。 –

0

試試這個

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { 
    let result = (textField.text as NSString?)?.replacingCharacters(in: range, with: string) ?? string 
    let text = result as String 
    if (text.range(of: "^(?i)(?![ ])(?!.*[ ]{2})[A-Z. ]*$", options: .regularExpression) != nil) { 

     return true 
    } 
    return false 
} 

如果您需要限制總長度更新的正則表達式到^(?i)(?![ ])(?!.*[ ]{2})[A-Z. ]{1,10}$10字符。根據需要更新長度。

0

它的工作原理!

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool 
{ 
     if (string == " " || string == " ") && range.location == 0 
     { 
      return false 
     } 

     return true 
} 
1

試試這個代碼:

//TextField Validation 
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool 
    { 
     if(textField == UserNameText) 
     { 
      if range.location == 0 && string == " " 
      { 
       return false 
      } 
      return true 
     } 
     else if(textField == PasswordText) 
     { 
      if range.location == 0 && string == " " 
      { 
       return false 
      } 
      return true 
     } 
     else 
     { 
      return true 
     } 
    } 

它不會允許用戶在第一封信給空間。例如,如果您需要爲username字段設置限制,其上限爲10個字符意味着使用此代碼:

//TextField Validation 
     func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool 
     { 
      if(textField == UserNameText) 
      { 
       if range.location == 0 && string == " " 
       { 
        return false 
       } 
       else if range.length + range.location > (self. UserNameText.text?.characters.count)! 
       { 
        return false 
       } 
       let NewLength = (self.let NewLength = (self.userEmailTxt.text?.characters.count)! - range.length 
       return NewLength <= 9 
      } 
      else if(textField == PasswordText) 
      { 
       if range.location == 0 && string == " " 
       { 
        return false 
       } 
       return true 
      } 
      else 
      { 
       return true 
      } 
     } 
0
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { 
    let rawString = string 
    let range = rawString.rangeOfCharacter(from: .whitespaces) 
    if ((textField.text?.characters.count)! == 0 && range != nil) 
    || ((textField.text?.characters.count)! > 0 && textField.text?.characters.last == " " && range != nil) { 
     return false 
    } 
return true 
} 
0

我找到答案終於爲我的要求

FUNC文本框(_文本框:的UITextField ,shouldChangeCharactersIn範圍:NSRange,replacementString字符串:字符串) - >布爾{

//for double space restrict 
    let str = textField.text 
    if str?.characters.count == 0 && string == " " { 
     return false 
    } 
    else if (str?.characters.count)! > 0 { 
     if str?.characters.last == " " && string == " " { 
      return false 
     } 
     else { 
      if textField.tag == 1 || textField.tag == 2 { //restrict text length 
       guard let text = textField.text else { return true } 
       let newLength = text.characters.count + string.characters.count - range.length 
       return newLength <= limitLengthForTextField 
      } 
     } 
    } 

    return true 
} 
0
func textField(_ textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { 
    if range.location == 0 && string == " " { 
     return false 
    } else if range.location > 1 && textField.text?.characters.last == " " && string == " " { 
     return false 
    }else{ 
     return true 
    } 
} 

嗨,安利請試試這個會解決你的問題。

相關問題