cjwirth / RichEditorView

RichEditorView is a simple, modular, drop-in UIView subclass for Rich Text Editing.
BSD 3-Clause "New" or "Revised" License
1.9k stars 445 forks source link

how to limit text count in the editor? #212

Closed RaviPulpstrategy closed 5 years ago

jahid1297 commented 5 years ago

@RaviPulpstrategy did you found any solution ?

RaviPulpstrategy commented 5 years ago

@RaviPulpstrategy did you found any solution ?

Not yet!

YoomamaFTW commented 5 years ago

Did you try checking the RichEditorDelegate? Here's what I've got:

var prevText: String!
@objc func richEditor(_ editor: RichEditorView, contentDidChange content: String) {
    if content.count > 20 {
        editor.html = prevText 
        // Where content is the HTML, not the visible text and 20 is the max number of characters
    } else {
        prevText = content
    }
}

I just started iOS development, so I'm not sure if this'll cause any issues regarding threading. I haven't tested copy and paste, but it should work just fine since the previous text onTextChange is saved in the variable.

Cheers!

YoomamaFTW commented 5 years ago

@RaviPulpstrategy @jahid1297

https://gist.github.com/YoomamaFTW/6f436e17f34f43ebda13e08d629e9d56

This is if you want the full code (it has some other issue I had, so sorry for the clump). The gist is the full programmatically made view controller of an editor view controller. The comment's code itself is the same for both storyboard and programmatically.

Do note: Make sure when you're inheriting classes that you also inherit RichEditorDelegate, so like this:

class EditorViewController: UIViewController, RichEditorDelegate {
    let editorView = RichEditorView()
    var prevText: String!
    override viewDidLoad() {
        super.viewDidLoad()
        editorView.delegate = self
        // Some other stuff
    }

    @objc func richEditor(_ editor: RichEditorView, contentDidChange content: String) {
        if content.count > 20 {
            editor.html = prevText 
            // Where content is the HTML, not the visible text and 20 is the max number of characters
        } else {
            prevText = content
        }
    }
}
RaviPulpstrategy commented 5 years ago

Yes, it will work! Thanks