| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- //
- // HXInputTextView.swift
- // bugu
- //
- // Created by Bugu on 2023/12/11.
- // Copyright © 2023 Bugu. All rights reserved.
- //
- import Foundation
- import UIKit
- class HXInputTextView: HXTextView {
-
- override func paste(_ sender: Any?) {
- guard let text = UIPasteboard.general.string, text.isNotEmpty else { return }
-
- let emojiText = EmojiHelper.replaceEmojiImage(textView: self, text: text)
- if attributedText.length == 0 {
- let attributedText = NSMutableAttributedString(attributedString: attributedText)
- attributedText.append(emojiText)
- self.attributedText = attributedText
- } else {
- textStorage.beginEditing()
- // Paste over selected text
- textStorage.insert(emojiText, at: selectedRange.location)
- textStorage.endEditing()
-
- // Advance the range to the selected range plus the number of characters added
- let location = selectedRange.location + emojiText.length
- selectedRange = NSRange(location: location, length: 0)
- }
-
- delegate?.textViewDidChange?(self)
- }
-
- public func cleanTextView() {
- self.attributedText = nil
- self.text = ""
- }
-
- public func appendText(text: String) {
- let emojiText = EmojiHelper.replaceEmojiImage(textView: self, text: text)
- textStorage.beginEditing()
- // Paste over selected text
- textStorage.insert(emojiText, at: selectedRange.location)
- textStorage.endEditing()
-
- // Advance the range to the selected range plus the number of characters added
- let location = selectedRange.location + emojiText.length
- selectedRange = NSRange(location: location, length: 0)
-
- delegate?.textViewDidChange?(self)
- }
-
- }
|