Swift: 有没有什么方法可以让用户在键盘上敲击某个键?
Swift: Is there a method for when someone taps a key on the keyboard?
在 Swift 的文本字段中点击键盘上的任何按钮时,有没有一种方法可以使用?当点击任何在 UITextField 中输入的键(不包括大写锁定和表情符号/切换语言键等键)时,我该怎么做(例如在 UITextField 的右视图中显示图像)?
如果是 UITextfield 和 UITextView,键盘通常会出现,尝试使用下面提到的委托。
所以,对于 UITextField
- (BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string {
// Do something here...
}
当你使用键盘时如果是 UITextView :
- (BOOL)textView:(UITextView *)textView
shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text {
// Do something here...
}
记住这一点,每次用户输入某个字符或按下键盘上的某个键时,此方法都会调用,您会知道他从键盘上选择的文本。
你可以使用这个委托来控制它:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
// String 键盘上新输入的字符,“”代表删除
//返回true会添加进去,返回false会忽略掉
return true
}
是的,你可以在 viewDidLoad 中添加 addTarget
override func viewDidLoad() {
// do your works
self.yourtextfield.addTarget(self, action: #selector(textFieldChanged), for: .editingChanged)
}
@objc func textFieldChanged() {
// do your work here
}
什么是addTarget?
Associates a target object and action method with the control.
你在addTarget中写下你想要的动作(即编辑改变,文本域确实开始编辑,或结束编辑)和一个选择器方法关联,当控件调用时(这里是编辑改变),这个选择器方法将被调用并且您可以根据需要处理有关控件的代码
您可以通过两种方式执行此操作:
UITextFieldDelegate
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
// Observer changes in text....
return true
}
目标行动机制:
在您的 viewDidLoad 中添加以下代码:
self.textfield.addTarget(self, action: #selector(textFieldTextChanged), for: .editingChanged)
和实现方法:
@objc func textFieldTextChanged() {
print(self.textfield.text!)
}
已更新:
class ViewController: UIViewController {
@IBOutlet weak var textField: UITextField!
let rightView = UIView(frame: CGRect(x: -10, y: 0, width: 20, height: 20))
override func viewDidLoad() {
super.viewDidLoad()
rightView.backgroundColor = .red
textField.rightView = rightView
textField.rightViewMode = .always
}
}
extension ViewController : UITextFieldDelegate {
func textField(_ textField: UITextField,
shouldChangeCharactersIn range: NSRange,
replacementString string: String) -> Bool {
if let text = textField.text, let textRange = Range(range, in: text) {
let updatedText = text.replacingCharacters(in: textRange, with: string)
if updatedText == "" {
textField.rightView = nil
} else {
textField.rightView = rightView
}
}
return true
}
}
你可以很容易做到
代码如下:
extension ViewController : UITextFieldDelegate {
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let userImageView = UIImageView(image: UIImage(named: "user"))
userImageView.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
self.txtField.rightViewMode = .always
if let textFieldValue = textField.text,
let textFieldRange = Range(range, in: textFieldValue) {
let newText = textFieldValue.replacingCharacters(in: textFieldRange, with: string)
if newText == "" {
textField.rightView = nil
} else {
textField.rightView = userImageView
}
}
return true
}
}
输出:
如果您在这方面遇到任何困难,请告诉我。
我的自定义清除按钮解决方案:
只需将此添加到 class
之外
extension ViewController: UITextFieldDelegate {
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let clearButton = UIImageView(frame:
CGRect(x: 7, y: 7, width: 16, height: 16))
clearButton.image = #imageLiteral(resourceName: "Clear")
let iconContainerView: UIView = UIView(frame:
CGRect(x: 0, y: 0, width: 37, height: 30))
iconContainerView.addSubview(clearButton)
iconContainerView.tintColor = #colorLiteral(red: 0.5568627451, green: 0.5568627451, blue: 0.5764705882, alpha: 1)
textField.rightView = iconContainerView
textField.rightViewMode = .whileEditing
if let textFieldValue = textField.text, let textFieldRange = Range(range, in: textFieldValue) {
let newText = textFieldValue.replacingCharacters(in: textFieldRange, with: string)
if newText == "" {
textField.rightView = nil
} else {
textField.rightView = iconContainerView
}
}
return true
}
}
在 Swift 的文本字段中点击键盘上的任何按钮时,有没有一种方法可以使用?当点击任何在 UITextField 中输入的键(不包括大写锁定和表情符号/切换语言键等键)时,我该怎么做(例如在 UITextField 的右视图中显示图像)?
如果是 UITextfield 和 UITextView,键盘通常会出现,尝试使用下面提到的委托。
所以,对于 UITextField
- (BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string {
// Do something here...
}
当你使用键盘时如果是 UITextView :
- (BOOL)textView:(UITextView *)textView
shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text {
// Do something here...
}
记住这一点,每次用户输入某个字符或按下键盘上的某个键时,此方法都会调用,您会知道他从键盘上选择的文本。
你可以使用这个委托来控制它:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
// String 键盘上新输入的字符,“”代表删除
//返回true会添加进去,返回false会忽略掉
return true
}
是的,你可以在 viewDidLoad 中添加 addTarget
override func viewDidLoad() {
// do your works
self.yourtextfield.addTarget(self, action: #selector(textFieldChanged), for: .editingChanged)
}
@objc func textFieldChanged() {
// do your work here
}
什么是addTarget?
Associates a target object and action method with the control.
你在addTarget中写下你想要的动作(即编辑改变,文本域确实开始编辑,或结束编辑)和一个选择器方法关联,当控件调用时(这里是编辑改变),这个选择器方法将被调用并且您可以根据需要处理有关控件的代码
您可以通过两种方式执行此操作:
UITextFieldDelegate
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { // Observer changes in text.... return true }
目标行动机制:
在您的 viewDidLoad 中添加以下代码:
self.textfield.addTarget(self, action: #selector(textFieldTextChanged), for: .editingChanged)
和实现方法:
@objc func textFieldTextChanged() {
print(self.textfield.text!)
}
已更新:
class ViewController: UIViewController {
@IBOutlet weak var textField: UITextField!
let rightView = UIView(frame: CGRect(x: -10, y: 0, width: 20, height: 20))
override func viewDidLoad() {
super.viewDidLoad()
rightView.backgroundColor = .red
textField.rightView = rightView
textField.rightViewMode = .always
}
}
extension ViewController : UITextFieldDelegate {
func textField(_ textField: UITextField,
shouldChangeCharactersIn range: NSRange,
replacementString string: String) -> Bool {
if let text = textField.text, let textRange = Range(range, in: text) {
let updatedText = text.replacingCharacters(in: textRange, with: string)
if updatedText == "" {
textField.rightView = nil
} else {
textField.rightView = rightView
}
}
return true
}
}
你可以很容易做到
代码如下:
extension ViewController : UITextFieldDelegate {
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let userImageView = UIImageView(image: UIImage(named: "user"))
userImageView.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
self.txtField.rightViewMode = .always
if let textFieldValue = textField.text,
let textFieldRange = Range(range, in: textFieldValue) {
let newText = textFieldValue.replacingCharacters(in: textFieldRange, with: string)
if newText == "" {
textField.rightView = nil
} else {
textField.rightView = userImageView
}
}
return true
}
}
输出:
如果您在这方面遇到任何困难,请告诉我。
我的自定义清除按钮解决方案:
只需将此添加到 class
之外extension ViewController: UITextFieldDelegate {
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let clearButton = UIImageView(frame:
CGRect(x: 7, y: 7, width: 16, height: 16))
clearButton.image = #imageLiteral(resourceName: "Clear")
let iconContainerView: UIView = UIView(frame:
CGRect(x: 0, y: 0, width: 37, height: 30))
iconContainerView.addSubview(clearButton)
iconContainerView.tintColor = #colorLiteral(red: 0.5568627451, green: 0.5568627451, blue: 0.5764705882, alpha: 1)
textField.rightView = iconContainerView
textField.rightViewMode = .whileEditing
if let textFieldValue = textField.text, let textFieldRange = Range(range, in: textFieldValue) {
let newText = textFieldValue.replacingCharacters(in: textFieldRange, with: string)
if newText == "" {
textField.rightView = nil
} else {
textField.rightView = iconContainerView
}
}
return true
}
}