English 中文(简体)
如何知道什么案文被贴在《美国国际法年鉴》中?
原标题:How to know when text is pasted into a UITextView?

什么事件是在将一整条案文打入了UITextView时发生的? 我需要修改我的文本框架。 案文的背后观点。

最佳回答

页: 1

 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

如果代表已经设立的话。 无论是在钥匙板上打上一种特性,还是在案文观点中贴出文字时,都是这样。 背后的案文是替代Text的论点。

http://developer.apple.com/library/ios/#documentation/uikit/reference/UITextViewDelegate_ Protocol/Reference/UITDextView.html#/pplea/subgate_refoc/intfele/

问题回答

这里指的是用来探测UIText的过去事件。 观点:

 // 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;
}

Checking the pasteboard s string by if string == UIPasteboard.general.string takes a couple of seconds if you have long sentence in the pasteboard. The user sees the keypad is frozen while this check. My solution is to check if the length of new characters is longer than 1. If it is longer than 1, the string is from the pasteboard.

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,超越这一职能。

public override func paste(_ sender: Any?) 

这一工作正在完善之中。

Xcode 11x Swift 5x

 func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    if text.contains(UIPasteboard.general.string ?? "") {
        return false
    }
    return true
}

When ever the user try to Paste into text field the if condition will execute
This code will stop pasting

页: 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
}

textView(_, shouldChangeTextIn,顶替Text)可在许多情况下使用。 您将读到<条码>查询表>。 用户每次都会看到系统警示,每次打字后,都使用过过去板的应用程序。

为了避免出现这一问题,您可在其中设立UITextView和超标的子级<>paste(_)。 该系统每当用户试图复制一件事时,都会要求这一功能。

接下来请打电话textView(_,elChangeTextIn,顶替Text)处理过去事件。

var isPastingContent = false // helper variable

open override func paste(_ sender: Any?) {
    isPastingContent = true // next call of `shouldChangeTextIn` will be for the paste action
    super.paste(sender)
}

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    if isPastingContent { // if we detected paste event
         // paste detected, do what you want here
         let pasteboardContent = UIPasteboard.general.string // you can get pasteboard content here safely. System alert will be shown only once.
         isPastingContent = false // toggle helper value back to false
    }
}

carlos16196是一个好的做法,但我也将通过修改<条码>加以 t弄[案文为Equal ToString:[IDPaste板上载]至<条码>。

通过这样做,你将发现,在文本概览中,其他类型文本没有列入UIPaste纸面的用户过去。

这是法典:

-(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;
}

ICT 13 with Combine

public extension UITextView {
    var textDidChangePublisher: AnyPublisher<String, Never> {
        NotificationCenter.default
            .publisher(for: UITextView.textDidChangeNotification, object: self)
            .compactMap { $0.object as? UITextView }
            .compactMap(.text)
            .eraseToAnyPublisher()
    }
    
    var attributedTextDidChangePublisher: AnyPublisher<NSAttributedString, Never> {
        NotificationCenter.default
            .publisher(for: UITextView.textDidChangeNotification, object: self)
            .compactMap { $0.object as? UITextView }
            .compactMap(.attributedText)
            .eraseToAnyPublisher()
    }
}
var cancellable = Set<AnyCancellable>()

textView.textDidChangePublisher
    .removeDuplicates()
    .sink { [weak self] newValue in
        guard let self = self else { return }
        // what you want
    }
    .store(in: &cancellable)

这是我能够开展工作的唯一途径。 我使用了一个案文领域,但同样的概念仍然应当适用于一项案文意见。

在<条码>中 在中,在我下面的“代表”方法中,将<>string的论据排在“国家统计”中,随后又回到“Sting”。 然后,我把它与过去相比。 除此以外,其他一切都载于上述法典。

// 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
    }
}




相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签