Post

Replies

Boosts

Views

Activity

RichText(markdown) live editor using TextEditor
hi everyone, any thought on how to implement a RichText(markdown) live editor using the new TextEditor(text: AttributedString, selection: AttributedTextSelection)? Having issues like: how to get the location of the selected text relatived to the TextEditor bounds, which is used to show and position a floating toolbar. how to detect the cursor point is a new line and the content is "# ", (of couse now the user enter a space after the "#"), and if so, how to apply a H1 heading format to that line much more issues like this, but these two are how to get me started, thanks struct TextEditor8: View { @State var text: AttributedString @State var selection = AttributedTextSelection() @State var isShowFloatingToolbar = false @State var toolbarPosition: CGPoint = .zero init() { var text = AttributedString("Hello ✋🏻,Who is ready for Cooking?") let range = text.characters.indices(where: \.isUppercase) text[range].foregroundColor = .blue _text = State(initialValue: text) } var body: some View { ZStack(alignment: .topLeading) { GeometryReader { geo in TextEditor(text: $text, selection: $selection) .onChange(of: selection, perform: { newV in let v = text[newV] if v.characters.isEmpty { isShowFloatingToolbar = false } else { isShowFloatingToolbar = true toolbarPosition = CGPoint(x: 20, y: 20) // how to get CGPoint relatived to TextEditor from selection } print("vvvv", v.characters.isEmpty) }) } if isShowFloatingToolbar { FloatingToolbarAtSelection() .position(toolbarPosition) } } } }
1
0
738
Jun ’25
Textkit2 NSTextView get cursor coordinate
I need to render some view at cursor position, how can I get the current coordinate of cursor? var location = NSRange(location: textView.selectedRange().location, length: 0) // the following is not available textLayoutManager.boundingRect(forGlyphRange: location, in: textContainer) can we use Textkit2 in production? I think there are lots of methods missing in NSTextLayoutManager while available in NSLayoutManager.
1
0
1.5k
Nov ’21
RichText(markdown) live editor using TextEditor
hi everyone, any thought on how to implement a RichText(markdown) live editor using the new TextEditor(text: AttributedString, selection: AttributedTextSelection)? Having issues like: how to get the location of the selected text relatived to the TextEditor bounds, which is used to show and position a floating toolbar. how to detect the cursor point is a new line and the content is "# ", (of couse now the user enter a space after the "#"), and if so, how to apply a H1 heading format to that line much more issues like this, but these two are how to get me started, thanks struct TextEditor8: View { @State var text: AttributedString @State var selection = AttributedTextSelection() @State var isShowFloatingToolbar = false @State var toolbarPosition: CGPoint = .zero init() { var text = AttributedString("Hello ✋🏻,Who is ready for Cooking?") let range = text.characters.indices(where: \.isUppercase) text[range].foregroundColor = .blue _text = State(initialValue: text) } var body: some View { ZStack(alignment: .topLeading) { GeometryReader { geo in TextEditor(text: $text, selection: $selection) .onChange(of: selection, perform: { newV in let v = text[newV] if v.characters.isEmpty { isShowFloatingToolbar = false } else { isShowFloatingToolbar = true toolbarPosition = CGPoint(x: 20, y: 20) // how to get CGPoint relatived to TextEditor from selection } print("vvvv", v.characters.isEmpty) }) } if isShowFloatingToolbar { FloatingToolbarAtSelection() .position(toolbarPosition) } } } }
Replies
1
Boosts
0
Views
738
Activity
Jun ’25
NSTextView ignore specific char input
I have a NSTextView, and I want to forbid users from typing in some specific character like "K", (when "K" is pressed,ignore the input, and don't change the underlying textStorage). is there a way I can do it? appreciate very much
Replies
1
Boosts
0
Views
1.2k
Activity
Aug ’22
Textkit2 NSTextView get cursor coordinate
I need to render some view at cursor position, how can I get the current coordinate of cursor? var location = NSRange(location: textView.selectedRange().location, length: 0) // the following is not available textLayoutManager.boundingRect(forGlyphRange: location, in: textContainer) can we use Textkit2 in production? I think there are lots of methods missing in NSTextLayoutManager while available in NSLayoutManager.
Replies
1
Boosts
0
Views
1.5k
Activity
Nov ’21