Typing always (force) lowercase or uppercase - ios swift

戏子无情 提交于 2020-01-20 06:58:40

问题


How to force to type always lowercase or uppercase for some textfields in swift language?


回答1:


You should return false in if block because, you already updated the textfield.

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
        if textField.isEqual(textFieldBodrum) {
            textFieldBodrum.text = (textField.text as NSString).stringByReplacingCharactersInRange(range, withString: string.lowercaseString)
            return false
        } else if textField.isEqual(textFieldYalikavak) {
            textFieldYalikavak.text = (textField.text as NSString).stringByReplacingCharactersInRange(range, withString: string.uppercaseString)
            return false
        }
        return true
    }

If you don't want to effect other textfields you should return true end of the shouldChangeCharactersInRange function.




回答2:


The simplest solution is to set the text field's autocapitalizationType property to .None to default to lowercase input or .AllCharacters to default to uppercase input. The user can still use the shift key to change capitalization though.



来源:https://stackoverflow.com/questions/31989028/typing-always-force-lowercase-or-uppercase-ios-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!