ruddfawcett / Notepad

[iOS] A fully themeable markdown editor with live syntax highlighting.
http://rudd.fyi/notepad
MIT License
875 stars 105 forks source link

Storyboard support #53

Open Loootus opened 5 years ago

Loootus commented 5 years ago

I can't find one way using Notepad in storyboard.

ruddfawcett commented 5 years ago

Hi, you can use a few different approaches to use Notepad with a Storyboard:

  1. Set a custom class of "Notepad" on the UITextView in your Storyboard.
  2. Use an IBOutlet to connected your UITextView to your view controller file, and then extend that UITextView.
  3. Add a Notepad programmatically to a connected view in an .xib file or Storyboard by initializing it and then using someView.addSubview(notepad).
rivera-ernesto commented 4 years ago
  1. Set a custom class of "Notepad" on the UITextView in your Storyboard.

This doesn't seem to work.

ruddfawcett commented 4 years ago

Thanks for letting me know, @rivera-ernesto. Are you getting any errors or just no result?

rivera-ernesto commented 4 years ago

No results. I've been fighting with it yesterday. Is seems that Storage methods never get called.

I've been trying to set the theme in viewDidLoad (self.notepad.storage.theme = Theme("one-dark")). For that I had to change storage visibility to public private(set) var storage: Storage = Storage().

DivineDominion commented 4 years ago

Yeah, you're right: when you use Storyboards/Xibs, the init(withCoder:) variant is called. Since theme configuration is only supported by passing in theme info to the initializer, you effectively cannot customize the theme at all.

I would hide the knowledge, tbh, and change Notepad:

public class Notepad: UITextView {

    var storage: Storage = Storage()
    public var theme: Theme {
        get { return storage.theme } 
        set { storage.theme = newValue }
    }
// ...
}

That would be a more stable API that exposes theme configuration on the base object, while hiding knowledge about the implementation in Storage.

ruddfawcett commented 4 years ago

@DivineDominion is correct, init(withCoder:)is called with Storyboards, so you'll have to use something like they suggested. An obvious oversight on my part, unfortunately. I think at one time I was imagining that the theme could be an IBInspectable/IBDesignable... but alas I forgot...

Looks like this is addressed in your PR #57.

ruddfawcett commented 4 years ago

Oops, my bad — misread #57. Let me think on this, even though I saw your PR with programmatic themes #59, I think there should be a drag/drop way to use Notepad. Perhaps with IBInspectables, etc. I'll play around with it when I get a chance.

rivera-ernesto commented 4 years ago

I was trying to "add" functionality to an existing UITextView and noticed that it needs to be initialized with the custom container.

That may be the same reason why Notepads initialized through Storyboards don't work.