如何设置第一个字符是字符串的大写

时间:2018-07-15 09:10:14

标签: ios swift uitextfield

我是Swift的新手。我正在实现一个登录页面。

我在视图控制器中有多个文本字段。当我在文本字段中输入文本时,后面的第一个应更改为大写。

我知道键盘设置中的键盘选择,但我的目标甚至是无论用户如何输入文本,都应将首字母大写。

我尝试了很多方法,但是没有找到任何解决方法

2 个答案:

答案 0 :(得分:0)

尝试此代码:

    override func viewDidLoad() {
        super.viewDidLoad()
         textField.addTarget(self, action: #selector(valueChanged(sender:)), for: .editingChanged)
    }

    @objc func valueChanged(sender: UITextField){
        sender.text = sender.text?.capitalized
    }

答案 1 :(得分:0)

SWIFT 3 -

textFiled.autocapitalizationType = .sentences //  first character of First word of every sentence is Capital
textFiled.autocapitalizationType = .words //first character of every word will be Capital
textFiled.autocapitalizationType = .allCharacters //All Characters will be Capital

故事板

enter image description here