danielsaidi / RichTextKit

RichTextKit is a Swift SDK that helps you use rich text in Swift and SwiftUI.
MIT License
944 stars 126 forks source link

Editor bugged with an initial value #86

Closed dave0111 closed 9 months ago

dave0111 commented 1 year ago

For some reason, the editor is bugged when the text State variable is initialized to something other than an empty string

In the Demo project's EditorScreen, I changed

@State
private var text = NSAttributedString.empty

to

@State
private var text = NSAttributedString(string: "test")

After launching the editor, and highlighting the "test" text, selecting a styling button does not apply the relative style to the selected text, unless you first select "underline". Selecting any style other than "underline" will NOT apply that style to the highlighted text, but if the "underline" style is first selected and deselected right after, every other style can then be applied without problem.

Also, I'm not sure if I'm doing something wrong, but programmatically assigning a new NSAttributedString to the RichTextEditor's text binding, will NOT result in the editor displaying the updated value, that will instead remain unchanged.

I'm on 0.9.1, installed via Swift Package Manager, and the bug was found on:

danielsaidi commented 1 year ago

Hi @dave0111

I'm not currently working with this project. Did you manage to figure something out regarding why it behaves this way?

benlong100 commented 11 months ago

I'm also having troubles under iOS 17 being able to programmatically assign the text binding. It always comes up with the default NSAttributableString I created. Should I be able to, at any time, programmatically change the contents of the text binding and see the new text in the RichTextEditor? 'Cause I can't get that to work. It's a shame, as this looks like a wonderful project that is much-needed in the SwiftUI world!

benlong100 commented 11 months ago

Okay, it seems like this is maybe a Sonoma/Xcode 15/iOS 17 problem. First of all, under Sonoma, having the RichTextKit library in my project makes the debugger run incredibly slowly. Second, if I place breakpoints at certain places and step through my code I can sometimes get the RichTextEditor to load with my desired result. At other times it doesn't. So it seems like my code is okay, there's something else going on. Unfortunately, my iPad decided to update itself to iOS 17 so I had to upgrade my Mac. Otherwise, I'd try testing on an earlier Xcode/iOS version.

dave0111 commented 11 months ago

Unfortunately I could not figure out a solution with a default/initial value, but I found a workaround by just setting my string with context.setAttributedString(to:), on appear.

Also, setting the font size is very glitchy, not sure if I should create a new issue to go more into detail, but could not get that to work either.

benlong100 commented 11 months ago

Hey Dave,

Thanks for the update. I tried your setAttrutedString thing but it's not working. This feels like bugs/changes in the latest round of Xcode/MacOS/iOS. I'm toying with the idea of downgrading my iPad and Mac, but there's always the question of whether it's just ultimately a dead-end to go with a library that's no longer being developed.

I have, however got a consistently-working test project. I can't paste it in here, because it's got some embedded HTML that screws up the comment display. If you want to play with it, I could send it to you, though I'm not sure there's any way to PM here.

Ben

benlong100 commented 11 months ago

Okay, I figured out my problem. It's that NSAttributedStrings are non-mutating. So if you declare one, it's stuck with those contents. So far, I haven't figured out a way to declare it in one place, but fill it with content in another. So the problem is not RichTextKit.

dave0111 commented 11 months ago

If you declare an NSAttributedString, like you said, it can’t be changed, but you can use NSMutableAttributedString instead, which is mutable. If I remember correctly, NSMutableAttributedString has an initialiser that takes an NSAttributedString, and you can go back from the mutable to the regular attributed string in the same way

danielsaidi commented 11 months ago

@benlong100 @dave0111

Thank you for providing so much information! While I'm not currently working on this project (freelancer and have been working with clients and commercial projects) it's by no means abandoned.

Like @dave0111 says - you need an NSMutableAttributedString 👍 I will take the library for a test run on the latest macOS and Xcode versions when I have time, but will be happy to merge any fixes you come up with until then.