将文本块粘贴到 UITextView 时会触发什么事件?粘贴文本时,我需要修改 textView 的框架。
谢谢阅读。
这是我用来检测 UITextView 中的粘贴事件的方法:
// Set this class to be the delegate of the UITextView. Now when a user will paste a text in that textview, this delegate will be called.
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
// Here we check if the replacement text is equal to the string we are currently holding in the paste board
if ([text isEqualToString:[UIPasteboard generalPasteboard].string]) {
// code to execute in case user is using paste
} else {
// code to execute other wise
}
return YES;
}
如果粘贴板上的句子很长,则检查粘贴板的字符串if string == UIPasteboard.general.string
需要几秒钟。用户在此检查时看到键盘被冻结。我的解决方案是检查新字符的长度是否长于 1。如果长于 1,则字符串来自粘贴板。
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if string.characters.count > 1{
//User did copy & paste
}else{
//User did input by keypad
}
return true
}
你的 UITextView 将调用它的 UITextViewDelegate 方法
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
如果已设置委托。当在键盘上键入字符以及将文本粘贴到文本视图中时,都会调用此方法。粘贴的文本是 replacementText 参数。
这是完美的
Xcode 11x 斯威夫特 5x
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if text.contains(UIPasteboard.general.string ?? "") {
return false
}
return true
}
当用户尝试粘贴到文本字段时,if条件将执行
此代码将停止粘贴
尝试子类 UITextview,并覆盖此功能。
public override func paste(_ sender: Any?)
它适用于 Swift5.1
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if let paste = UIPasteboard.general.string, text == paste {
print("paste")
} else {
print("normal typing")
}
return true
}
在 iOS 14 中,每次应用程序从中获取值时都会触发通知,UIPasteboard.general.string
因此检测用户是否粘贴某些内容的正确方法是覆盖paste(_)
功能:
var isPastingContent = false
open override func paste(_ sender: Any?) {
isPastingContent = true
super.paste(sender)
}
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if isPastingContent {
// do something
}
isPastingContent = false
}
carlos16196 是一个很好的方法,但我也会通过更改[text isEqualToString:[UIPasteboard generalPasteboard].string]
为[text containsString:[UIPasteboard generalPasteboard].string]
通过这样做,您将检测到用户何时在文本视图中粘贴不在 UIPasteboard 中的其他键入文本之后。
这是代码:
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
// Here we check if the replacement text is equal to the string we are currently holding in the paste board
if ([text containsString:[UIPasteboard generalPasteboard].string]) {
// code to execute in case user is using paste
} else {
// code to execute other wise
}
return YES;
}
这是我能够让它工作的唯一方法。我使用了 textField,但同样的概念仍然适用于 textView。
在下面的shouldChangeCharactersIn
委托方法中,我将string
参数转换为 NSString,然后再转换回 String。然后我将它与粘贴的任何内容进行比较。其他所有内容都在代码上方的注释中。
// 1. class property for anything that was copied and will be pasted
var pasted: String?
// 2. When the user first taps the textfield set the above class property to the copied text (if there is any)
func textFieldDidBeginEditing(_ textField: UITextField) {
// 3. set it here
pasted = UIPasteboard.general.string // this is what was copied and will be pasted
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard let safeText = textField.text else { return true }
let currentString: NSString = safeText as NSString
let newString: NSString = currentString.replacingCharacters(in: range, with: string) as NSString
let str = newString as String
// 4. compare the above str constant to the pasted variable
if str == self.pasted {
print("pasted")
} else {
print("typed")
}
return true
}
func textFieldDidEndEditing(_ textField: UITextField) {
// 5. when the user is finished with the textField set the pasted property to nil
pasted = nil
}
这是我用来检测粘贴图像的方法:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if (UIPasteboard.generalPasteboard.image &&
[UIPasteboard.generalPasteboard.string.lowercaseString isEqualToString:text.lowercaseString]) {
//Pasted image
return NO;
}
return YES;
}
在 SWIFT 4 中:
func textViewDidChange(_ textView: UITextView) {
if(textView.text == UIPasteboard.general.string)
{
//Text pasted
}
}