代码之家  ›  专栏  ›  技术社区  ›  Kavin Kumar Arumugam

iOS Swift仅启用键盘中的退格键

  •  0
  • Kavin Kumar Arumugam  · 技术社区  · 8 年前

    textView .例如,在 description 我的文本限制为10000。当textView包含超过10000个字符时,我只需要启用键盘上的退格键,并需要禁用键盘上的所有其他键,是否可能。这是我的代码:

    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText string: String) -> Bool {
        if(textView == DescriptionText)
        {
          if range.length + range.location > (self.DescriptionText.text?.characters.count)!
          {
                return false
          }
          else if range.location == 0 && string == " "
          {
                return false
          }
          let NewLength = (self.DescriptionText.text?.characters.count)! - range.length
          return NewLength <= 9999
          }
          else
          {
              if range.location == 0 && string == " "
              {
                    return false
              }
              return true
          }
        }
    
    2 回复  |  直到 8 年前
        1
  •  1
  •   yoshiiiiiiii    8 年前

    在textfieldShouldChange中添加以下内容:

    if(range.length + range.location > textField.text.length)
        {
            return NO;
        }
    
        NSUInteger newLength = [textField.text length] + [string length] - range.length;
    
        return newLength <= 10000;
    
        2
  •  -1
  •   Dharma    8 年前

    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText string: String) -> Bool {
    
        let textString = (textView.text! as NSString).replacingCharacters(in: range, with: string)
    
        if textView == self.DescriptionText && string.characters.count > 0 {
            return textString.characters.count <= 10000 
        }
    
        return true
    }