UIViewRepresentable auto bracket closing

I made this simple UiViewRepresentable which displays line numbers and a super simple html syntax, (I am not going into detail here) but I wondered how I can make it so if I enter: “<” then the editor automatically enters: “>” and the cursors goes back one character. I am very new to UIKit and barely got this one working so help is nearly appreciated.

Here’s the editor code:

struct SourceEditor: UIViewRepresentable {
    @Binding var text: String
    @Binding var highlightText: String
    
    var theme: Theme
    
    func makeUIView(context: Context) -> SourceEditorClass {
        let textView = SourceEditorClass()
        textView.isEditable = true
        textView.delegate = context.coordinator
        textView.autocorrectionType = .no
        textView.autocapitalizationType = .none
        textView.keyboardType = .asciiCapable
        textView.font = UIFont.monospacedSystemFont(ofSize: 15, weight: .regular)
        textView.backgroundColor = UIColor(theme.backgroundColor)
        textView.textColor = UIColor(theme.textColor)
        textView.textContainer.lineBreakMode = .byWordWrapping
        
        // Add toolbar
        let toolbar = UIToolbar()
        toolbar.sizeToFit()
        
        let lessThanButton = UIBarButtonItem(title: "<", style: .plain, target: context.coordinator, action: #selector(context.coordinator.insertLessThan))
        let greaterThanButton = UIBarButtonItem(title: ">", style: .plain, target: context.coordinator, action: #selector(context.coordinator.insertGreaterThan))
        let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
        
        toolbar.items = [flexibleSpace, lessThanButton, greaterThanButton, flexibleSpace]
        
        textView.inputAccessoryView = toolbar
        
        return textView
    }
    
    func updateUIView(_ uiView: SourceEditorClass, context: Context) {
        context.coordinator.textDidChange = nil
        context.coordinator.selectionDidChange = nil
        
        if uiView.text != text {
            uiView.text = text
        }
        
        if uiView.theme != theme {
            uiView.backgroundColor = UIColor(theme.backgroundColor)
            uiView.textColor = UIColor(theme.textColor)
            uiView.theme = theme
        }
        
        context.coordinator.textDidChange = { newText in
            text = newText
        }
        context.coordinator.selectionDidChange = { text, selection in
            print(text.lineNumbersForRange(selection) ?? [])
            uiView.currentLines = text.lineNumbersForRange(selection) ?? []
            highlightText = ""
        }
        
        uiView.textStorage.addAttribute(.foregroundColor, value: UIColor(theme.textColor), range: NSRange(location: 0, length: uiView.text.count))
        uiView.textStorage.removeAttribute(.backgroundColor, range: NSRange(location: 0, length: uiView.text.count))
        uiView.textStorage.removeAttribute(.underlineStyle, range: NSRange(location: 0, length: uiView.text.count))
        
        if !highlightText.isEmpty {
            let ranges = findRegex(pattern: highlightText, text: uiView.text)
            for range in ranges {
                uiView.textStorage.addAttribute(.backgroundColor, value: UIColor.yellow.withAlphaComponent(0.8), range: range)
            }
        }
        
        for keyword in theme.keywords {
            let ranges = findRegex(pattern: keyword.regex, text: uiView.text)
            for range in ranges {
                if keyword.style == .foreground {
                    uiView.textStorage.addAttribute(.foregroundColor, value: UIColor(keyword.color), range: range)
                } else if keyword.style == .background {
                    uiView.textStorage.addAttribute(.backgroundColor, value: UIColor(keyword.color), range: range)
                } else if keyword.style == .underline {
                    uiView.textStorage.addAttribute(.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: range)
                    uiView.textStorage.addAttribute(.underlineColor, value: UIColor(keyword.color), range: range)
                }
            }
        }
    }
    
    func makeCoordinator() -> Coordinator {
        return Coordinator(self)
    }

    class Coordinator: NSObject, UITextViewDelegate {
        
        var parent: SourceEditor
        var textDidChange: ((String) -> ())? = nil
        var selectionDidChange: ((_ text: String, _ selection: NSRange) -> ())? = nil
        
        init(_ parent: SourceEditor) {
            self.parent = parent
        }

        func textViewDidChange(_ textView: UITextView) {
            textDidChange?(textView.text)
        }

        func textViewDidChangeSelection(_ textView: UITextView) {
            selectionDidChange?(textView.text, textView.selectedRange)
            textView.setNeedsLayout()
        }
        
        @objc func insertLessThan() {
            insertText("<")
        }
        
        @objc func insertGreaterThan() {
            insertText(">")
        }
        
        private func insertText(_ text: String) {
            if let textView = UIResponder.currentFirstResponder as? UITextView {
                if let selectedRange = textView.selectedTextRange {
                    textView.replace(selectedRange, withText: text)
                }
            }
        }
    }
}

Please note that this code won’t run on its own because of the other requirements in the file. If you need it to fully test your code I can gladly give the full code including all files which are required to run it. Thanks in advance!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật