diff --git a/RichEditorView/Classes/RichEditorOptionItem.swift b/RichEditorView/Classes/RichEditorOptionItem.swift index 7ffee8fe..8749d16b 100755 --- a/RichEditorView/Classes/RichEditorOptionItem.swift +++ b/RichEditorView/Classes/RichEditorOptionItem.swift @@ -37,12 +37,16 @@ public struct RichEditorOptionItem: RichEditorOption { /// The action to be performed when tapped public var handler: ((RichEditorToolbar) -> Void) - public init(image: UIImage?, title: String, action: @escaping ((RichEditorToolbar) -> Void)) { + public init(image: UIImage? = nil, title: String, action: @escaping ((RichEditorToolbar) -> Void)) { self.image = image self.title = title self.handler = action } + public init(title: String, action: @escaping ((RichEditorToolbar) -> Void)) { + self.init(image: nil, title: title, action: action) + } + // MARK: RichEditorOption public func action(_ toolbar: RichEditorToolbar) { @@ -114,7 +118,7 @@ public enum RichEditorDefaultOption: RichEditorOption { } let bundle = Bundle(for: RichEditorToolbar.self) - return UIImage(named: name, in: bundle, compatibleWith: nil) + return UIImage(named: name, in: bundle, compatibleWith: nil)?.withRenderingMode(.alwaysTemplate) } public var title: String { diff --git a/RichEditorView/Classes/RichEditorView.swift b/RichEditorView/Classes/RichEditorView.swift index 693a204f..e8ba5153 100755 --- a/RichEditorView/Classes/RichEditorView.swift +++ b/RichEditorView/Classes/RichEditorView.swift @@ -229,15 +229,15 @@ private let DefaultInnerLineHeight: Int = 21 /// Whether or not the selection has a type specifically of "Range". public func hasRangeSelection(handler: @escaping (Bool) -> Void) { - runJS("RE.rangeSelectionExists()") { r in - handler(r == "true" ? true : false) + runJS("RE.rangeSelectionExists()") { val in + handler((val as NSString).boolValue) } } /// Whether or not the selection has a type specifically of "Range" or "Caret". public func hasRangeOrCaretSelection(handler: @escaping (Bool) -> Void) { - runJS("RE.rangeOrCaretSelectionExists()") { r in - handler(r == "true" ? true : false) + runJS("RE.rangeOrCaretSelectionExists()") { val in + handler((val as NSString).boolValue) } } diff --git a/RichEditorViewSample/RichEditorViewSample/ViewController.swift b/RichEditorViewSample/RichEditorViewSample/ViewController.swift index 0d0f76ad..feb6aa8e 100644 --- a/RichEditorViewSample/RichEditorViewSample/ViewController.swift +++ b/RichEditorViewSample/RichEditorViewSample/ViewController.swift @@ -16,6 +16,13 @@ class ViewController: UIViewController { lazy var toolbar: RichEditorToolbar = { let toolbar = RichEditorToolbar(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 44)) +// let options: [RichEditorDefaultOption] = [ +// .bold, .italic, .underline, +// .unorderedList, .orderedList, +// .indent, .outdent, +// .textColor, .textBackgroundColor, +// .undo, .redo, +// ] toolbar.options = RichEditorDefaultOption.all return toolbar }() @@ -32,13 +39,13 @@ class ViewController: UIViewController { toolbar.editor = editorView // This will create a custom action that clears all the input text when it is pressed -// let item = RichEditorOptionItem(image: nil, title: "Clear") { toolbar in -// toolbar?.editor?.html = "" -// } -// -// var options = toolbar.options -// options.append(item) -// toolbar.options = options + let item = RichEditorOptionItem(title: "Clear") { toolbar in + toolbar.editor?.html = "" + } + + var options = toolbar.options + options.append(item) + toolbar.options = options } } @@ -113,9 +120,11 @@ extension ViewController: RichEditorToolbarDelegate, UIColorPickerViewController func richEditorToolbarInsertLink(_ toolbar: RichEditorToolbar) { // Can only add links to selected text, so make sure there is a range selection first -// if let hasSelection = toolbar.editor?.rangeSelectionExists(), hasSelection { -// toolbar.editor?.insertLink("https://github.com/cbess/RichEditorView", title: "GitHub Link") -// } + toolbar.editor?.hasRangeSelection(handler: { (hasSelection) in + if hasSelection { + self.toolbar.editor?.insertLink("https://github.com/cbess/RichEditorView", title: "GitHub Link") + } + }) } func colorPickerViewControllerDidSelectColor(_ viewController: UIColorPickerViewController) {