Skip to content

Instantly share code, notes, and snippets.

@YoomamaFTW
Last active October 27, 2019 19:50
Show Gist options
  • Save YoomamaFTW/6f436e17f34f43ebda13e08d629e9d56 to your computer and use it in GitHub Desktop.
Save YoomamaFTW/6f436e17f34f43ebda13e08d629e9d56 to your computer and use it in GitHub Desktop.
RIchEditorView with Max Character Count Functionality (HTML) and Cursor Floating Under Keyboard on focus Temporary Fix (Swift 4 and 5)
import UIKit
import RichEditorView. // TODO: Download https://github.com/cbess/RichEditorView for Swift 5 support
class EditorViewController: UIViewController, RichEditorDelegate {
let editorView = RichEditorView()
var isEditingText: Bool!
var initString: String!
var prevText: String!
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
editorView.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)
editorView.setEditorBackgroundColor(.green)
editorView.delegate = self
editorView.placeholder = "Type some stuff out. Don't be afraid."
if isEditingText != nil { // Designed so that RichEditorView can be used for editing or only for viewing.
editorView.isEditingEnabled = isEditingText
}
let toolbar = RichEditorToolbar(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 44))
toolbar.options = RichEditorDefaultOption.all
toolbar.editor = editorView
editorView.inputAccessoryView = toolbar
view.backgroundColor = .white
view.addSubview(editorView)
if initString == nil {editorView.html = ""} else {editorView.html = initString}
}
// MARK: Reframe the editor so that the cursor does not float underneath the keyboard
@objc func keyboardWillShow(notification: NSNotification) {
// TODO: Add navigation DONE button to unFocus
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
let keyboardHeight = keyboardSize.height
editorView.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height - 60 - keyboardHeight)
}
}
@objc func richEditor(_ editor: RichEditorView, contentDidChange content: String) {
if editor.isFocused {
if content.count > 20 {
editor.html = prevText
// print("Stop") This was for testing. The 20 is the max number of characters in HTML, not the visible string.
} else {
// print(content)
prevText = content
}}
}
}
@YoomamaFTW
Copy link
Author

Sorry, this was a simple copy and paste. The gist was mainly designed for the Max Character count functionality. There is also an issue with the cursor going underneath the keyboard due to framing, and I made a temporary fix because I can only assume what the frame size of an iPhone vs. iPad could be. cjwirth/RichEditorView#212

@YoomamaFTW
Copy link
Author

This should work for both storyboard and programmatically (this editor was made programmatically).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment