kyle-n / HighlightedTextEditor

A SwiftUI view for dynamically highlighting user input
MIT License
704 stars 67 forks source link

ParagraphStyle not working #10

Closed xiaoxidong closed 3 years ago

xiaoxidong commented 3 years ago

I want to change the paragraph spacing and line spacing, and I set the defaultParagraphStyle for the textView, but it seems not working. Here is the code add to the textView.

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 10
paragraphStyle.paragraphSpacing = 10
textView.defaultParagraphStyle = paragraphStyle

Thank you so much.

kyle-n commented 3 years ago

HLTE never meant to support paragraph styles, so marking this as a feature request.

kyle-n commented 3 years ago

I take it back. .paragraphStyle is on NSAttributedString.Key, so you can actually do this already.

let all = try! NSRegularExpression(pattern: "^.*$", options: [.dotMatchesLineSeparators])

struct ContentView: View {
    @State private var text: String = "**bold** \nline two"

    var body: some View {
        VStack {
            HighlightedTextEditor(text: $text, highlightRules: [
                HighlightRule(pattern: all, formattingRule: TextFormattingRule(key: .paragraphStyle, value: grafStyle))
            ])
        }
    }

    private var grafStyle: NSParagraphStyle {
        let style = NSMutableParagraphStyle()
        style.lineSpacing = 20
        style.paragraphSpacing = 40
        return style
    }
}
xiaoxidong commented 3 years ago

Thank you so much, I am new to swift and learning swiftUI. Really help me a lot.

kyle-n commented 3 years ago

No problem! Happy to help, everybody has to start somewhere.