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)
}
}
}
}
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
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
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.
I can't get NSTextView to work in textkit2 with the following code, textView.textContentStorage , textContentStorage.textStorage is both nil. does anyone have a working example? hope there is a Frameworks Engineer help me out, appreciate so much.
import AppKit
open class MyEditor: NSView {
let textView: NSTextView
let textLayoutManager: NSTextLayoutManager
let textContentStorage: NSTextContentStorage
public init() {
textLayoutManager = NSTextLayoutManager()
textContentStorage = NSTextContentStorage()
let textContainer = NSTextContainer(size: NSSize(width: 200, height: 0.0))
textContainer.widthTracksTextView = true
textContainer.heightTracksTextView = true
textLayoutManager.textContainer = textContainer
textContentStorage.addTextLayoutManager(textLayoutManager)
textView = NSTextView(frame: .zero, textContainer: textContainer)
super.init(frame: .zero)
addSubview(textView)
}
public func setAttributedString(_ attrString: NSAttributedString) {
print(textView.textContentStorage) // always nil
textContentStorage.textStorage?.setAttributedString(attrString)
}
public required init?(coder: NSCoder) {
return nil
}
}
Swift
private struct MyView: View {
@State var text: String = "textfield value."
func onEditingChanged (isEditing: Bool) - Void {
print("changed \(isEditing)")
}
func onCommit() - Void {
print("commited")
NSApplication.shared.sendAction(#selector(NSResponder.resignFirstResponder), to:nil, from:nil)
}
var body: some View {
HStack {
TextField("x", text: $text, onEditingChanged: onEditingChanged, onCommit: onCommit)
.textFieldStyle(PlainTextFieldStyle())
.disableAutocorrection(true)
}
}
}
After I finish entering on TextField and hit return key on my keyborad,I'm expecting the textfield to lose focus. However,it won't, and instead, all text in the textfield is selected? is there some workaround ?
I'm trying to create a horizontal ScrollView, inside which there are many Lists that can scroll vertically.
like MacOS Finder app The problem is, horizontal scroll is not happening if mouse scroll is triggered inside List.
My guess is that scroll event is captured by List, ScrollView has no chance to handle.
ScrollView(.horizontal) {
List
List
List
...
}
Does any has any workaround?