有没有办法快速跟踪用户输入的字符是否位于两个其他字符之间?

发布于 2025-01-10 05:37:08 字数 2551 浏览 0 评论 0原文

我正在开发一个应用程序,它具有以下功能:当用户按下快捷按钮时,UITextView 会输入相应的符号(如“**”、“__”或“”“”)。然后光标返回到这对符号之间的位置,用户可以输入想要的内容。 当用户完成后,他可以按“输入”按钮跳出该对符号并输入其他内容。 但如果用户手动更改光标,例如将光标移出符号。他按 Enter 键然后显示正常的换行。

我主要完成了这个功能。但我不知道如何跟踪光标是否仍在这对符号之间。

@objc func shortcutFunc(sender: CustomBtn, forEvent: UIEvent) {
    insertFromCursor(sender: sender, forEvent: forEvent)
    
    retreat = sender.retreat!

    var selectedView: CustomTextView?

    if bodyViewUnderEditing {
        selectedView = textField.bodyView
    } else if titleViewUnderEditing {
        selectedView = textField.titleView
    }
    if let selectedView = selectedView {
        let location = selectedView.selectedRange.location
        selectedView.selectedRange = NSRange(location: location - sender.retreat!, length: 0)
        isShortcutBtnInputing = true
    }
}

@objc func insertFromCursor(sender: CustomBtn, forEvent event: UIEvent) {
    var selectedView: CustomTextView?

    if bodyViewUnderEditing {
        selectedView = textField.bodyView
    } else if titleViewUnderEditing {
        selectedView = textField.titleView
    }

    if let selectedView = selectedView {
        let range = selectedView.selectedRange
        let start = selectedView.position(from: selectedView.beginningOfDocument, offset: range.location)!
        let end = selectedView.position(from: start, offset: range.length)!
        let textRange = selectedView.textRange(from: start, to: end)!
        selectedView.replace(textRange, withText: sender.argument!)
    }
}

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    if textView == textField.bodyView {
        if text == "\n", isShortcutBtnInputing, bodyViewUnderEditing {
            let location = textField.bodyView.selectedRange.location
            textField.bodyView.selectedRange = NSRange(location: location + retreat, length: 0)
            isShortcutBtnInputing = false

            return false
        }
        return true

    } else if textView == textField.titleView {
        if text == "\n" {
            if isShortcutBtnInputing, titleViewUnderEditing {
                let location = textField.titleView.selectedRange.location
                textField.titleView.selectedRange = NSRange(location: location + retreat, length: 0)
                isShortcutBtnInputing = false
            } else {
                textField.bodyView.becomeFirstResponder()
            }
            return false
        }
        return true
    }
    return true
}

I'm developing an App, which has this feature: when user press a shortcut button, the UITextView is entered into the corresponding symbol (like "**", "__" or """"). Then the cursor turn back to the position between the pair of symbols, and user input what the want.
When user has done, he can press the "enter" button to jump out of the pair of symbol and input sth else.
But if the user change the cursor manually, for example he move the cursor out of the symbols.He press enter then display a normal line feed.

I have mostly done this function. But I don't know how to track if the cursor is still between the pair of symbols.

@objc func shortcutFunc(sender: CustomBtn, forEvent: UIEvent) {
    insertFromCursor(sender: sender, forEvent: forEvent)
    
    retreat = sender.retreat!

    var selectedView: CustomTextView?

    if bodyViewUnderEditing {
        selectedView = textField.bodyView
    } else if titleViewUnderEditing {
        selectedView = textField.titleView
    }
    if let selectedView = selectedView {
        let location = selectedView.selectedRange.location
        selectedView.selectedRange = NSRange(location: location - sender.retreat!, length: 0)
        isShortcutBtnInputing = true
    }
}

@objc func insertFromCursor(sender: CustomBtn, forEvent event: UIEvent) {
    var selectedView: CustomTextView?

    if bodyViewUnderEditing {
        selectedView = textField.bodyView
    } else if titleViewUnderEditing {
        selectedView = textField.titleView
    }

    if let selectedView = selectedView {
        let range = selectedView.selectedRange
        let start = selectedView.position(from: selectedView.beginningOfDocument, offset: range.location)!
        let end = selectedView.position(from: start, offset: range.length)!
        let textRange = selectedView.textRange(from: start, to: end)!
        selectedView.replace(textRange, withText: sender.argument!)
    }
}

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    if textView == textField.bodyView {
        if text == "\n", isShortcutBtnInputing, bodyViewUnderEditing {
            let location = textField.bodyView.selectedRange.location
            textField.bodyView.selectedRange = NSRange(location: location + retreat, length: 0)
            isShortcutBtnInputing = false

            return false
        }
        return true

    } else if textView == textField.titleView {
        if text == "\n" {
            if isShortcutBtnInputing, titleViewUnderEditing {
                let location = textField.titleView.selectedRange.location
                textField.titleView.selectedRange = NSRange(location: location + retreat, length: 0)
                isShortcutBtnInputing = false
            } else {
                textField.bodyView.becomeFirstResponder()
            }
            return false
        }
        return true
    }
    return true
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文