UIViewRepresentable cursor bugging

I made this simple uiviewrepresentable which put brackets < and > however sometimes when the text changes ( characters get added ) the cursor jumps up for a split second and back down again. I am new to UIKit and can’t fix it. Help is greatly appreciated.
I tried a lot of solutions but can’t get it to work.

Here is my code for the editor. I will not provide the code for the editor class because that works fine and never caused any issues. However if you need it to get the entire thing working I will gladly provide it.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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
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()
}
class Coordinator: NSObject, UITextViewDelegate {
var textDidChange: ((String) -> ())? = nil
var selectionDidChange: ((_ text: String, _ selection: NSRange) -> ())? = nil
func textViewDidChange(_ textView: UITextView) {
textDidChange?(textView.text)
}
func textViewDidChangeSelection(_ textView: UITextView) {
selectionDidChange?(textView.text, textView.selectedRange)
textView.setNeedsLayout()
}
}
}
</code>
<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 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() } class Coordinator: NSObject, UITextViewDelegate { var textDidChange: ((String) -> ())? = nil var selectionDidChange: ((_ text: String, _ selection: NSRange) -> ())? = nil func textViewDidChange(_ textView: UITextView) { textDidChange?(textView.text) } func textViewDidChangeSelection(_ textView: UITextView) { selectionDidChange?(textView.text, textView.selectedRange) textView.setNeedsLayout() } } } </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
        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()
    }

    class Coordinator: NSObject, UITextViewDelegate {
        
        var textDidChange: ((String) -> ())? = nil
        var selectionDidChange: ((_ text: String, _ selection: NSRange) -> ())? = nil

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

        func textViewDidChangeSelection(_ textView: UITextView) {
            selectionDidChange?(textView.text, textView.selectedRange)
            textView.setNeedsLayout()
        }
    }
}

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