当在键盘上轻按键盘上的任何按钮以将输入内容放入Swift的文本字段时,是否可以使用一种方法?
当任何将输入内容输入到UITextField中的键(例如,不包括大写锁定键和emoji表情/切换语言键之类的键)被敲击时,我该怎么办(例如,在UITextField的rightView中显示图像)?>答案 0 :(得分:0)
在使用以下提到的委托的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...
}
请记住,每次用户在键盘上输入某些字符或按下某些键时,都会调用此方法,您将知道他从键盘上选择的文本。
答案 1 :(得分:0)
您可以使用此委托来控制它:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
// String 键盘上新输入的字符,“”代表删除
//返回true会添加进去,返回false会忽略掉
return true
}
答案 2 :(得分:0)
是的,您可以通过在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?
将目标对象和操作方法与控件相关联。
您写下了addTarget,它将与您想要的动作(即编辑更改,文本字段开始编辑或结束编辑)和选择器方法相关联,当控件被调用(此处为编辑更改)时,将调用该选择器方法,并且可以处理您想要的有关控件的代码
答案 3 :(得分:0)
您可以通过两种方式执行此操作:
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
}
}
答案 4 :(得分:0)
您可以轻松完成
代码如下:
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
}
}
输出:
如果您在此方面遇到任何困难,请告诉我。
答案 5 :(得分:0)
我的自定义清除按钮解决方案:
只需将其添加到班级之外
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
}
}