Disable return key in UITextView

后端 未结 5 1104
小鲜肉
小鲜肉 2021-02-06 23:55

I am trying to disable the return key found when typing in a UITextView. I want the text to have no page indents like found in a UITextField. This is t

5条回答
  •  北荒
    北荒 (楼主)
    2021-02-07 00:33

    In Swift 3+ add:

    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        guard text.rangeOfCharacter(from: CharacterSet.newlines) == nil else {
            // textView.resignFirstResponder() // uncomment this to close the keyboard when return key is pressed
            return false
        }
    
        return true
    }
    

    Don't forget to add textView's delegate in order for this to be called

提交回复
热议问题