ios - 警告文本字段中的大写 [仅第一个字]

标签 ios swift xcode

我正在开发一个 list 应用程序。如果用户想在他的 list 上添加一个新点,他可以按下“添加”按钮,然后会出现一个带有 UITextField 的警告以将他的新任务放入。

alert.addAction(UIAlertAction(title: "Add", style: .default, handler: { (action) in

let textf = alert.textFields![0] as UITextField

        if(textf.text != "")
        {
            checklist.append(textf.text!.capitalized)
        }
        self.myTableView.reloadData()
    }))
    self.present(alert, animated: true, completion: nil)
}

这样做会在点击“添加”后将单词大写。但我只希望第一个单词大写。另外,我希望它在出现时出现在键盘上,以便第一个单词立即大写。我该怎么做?

let alert = UIAlertController(title: "Add new task", message: nil, preferredStyle: .alert)
    alert.addTextField { (alertInputTextField) in

    }

最佳答案

使用文本框的autocapitalizationType属性,在添加的时候设置为句子...

alert.addTextField { textField in
    textField.text = "Some default text"
    textField.autocapitalizationType = .sentences 
}

始终引用 documentation ,它很好地解释了这一点。

如果您需要直接对字符串执行此操作,只需将字符串的第一个字母大写即可。

关于ios - 警告文本字段中的大写 [仅第一个字],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53375726/

相关文章:

ios - 在 iOS 上将 font-awesome 与 Phonegap 结合使用

ios - 获取登录用户的 Facebook 好友列表

xcode - 无法在 OSX 10.15.4 上使用 asdf 安装 Erlang

c# - MonoTouch-XCode的组织器中更好的控制台输出

ios - 什么时候从 iOS 设备发送答案数据?

iOS强制App使用自定义键盘

swift - Swift 中的负随机数

ios - 如何在 Swift 中识别 iPhone SHAKE?

ios - 试图在自己的 willSet 中存储属性 ''

ios - Xcode中如何获取UI对象