diff --git a/Proton/Sources/Swift/Editor/EditorView.swift b/Proton/Sources/Swift/Editor/EditorView.swift index c29d580c..8828b324 100644 --- a/Proton/Sources/Swift/Editor/EditorView.swift +++ b/Proton/Sources/Swift/Editor/EditorView.swift @@ -152,6 +152,11 @@ open class EditorView: UIView { /// mat be more than when synchronous mode, ie default, is used. The perceived performance/TTI will almost always be better with asynchronous rendering. public weak var asyncAttachmentRenderingDelegate: AsyncAttachmentRenderingDelegate? + /// Returns `UITextInput` of current instance + public var textInput: UITextInput { + richTextView + } + public var textInteractions: [UITextInteraction] { richTextView.interactions.compactMap({ $0 as? UITextInteraction }) } @@ -740,6 +745,18 @@ open class EditorView: UIView { return richTextView.becomeFirstResponder() } + /// Denotes of the Editor is first responder + /// - Returns: true, if is first responder + public func isFirstResponder() -> Bool { + richTextView.isFirstResponder + } + + /// Resets typing attributes back to default text color, font and paragraph style. + ///All other attributes are dropped. + public func resetTypingAttributes() { + richTextView.resetTypingAttributes() + } + /// Converts given range to `UITextRange`, if valid /// - Parameter range: Range to convert /// - Returns: `UITextRange` representation of provided NSRange, if valid. diff --git a/Proton/Sources/Swift/Helpers/NSRangeExtensions.swift b/Proton/Sources/Swift/Helpers/NSRangeExtensions.swift index d0f68995..f712c3b8 100644 --- a/Proton/Sources/Swift/Helpers/NSRangeExtensions.swift +++ b/Proton/Sources/Swift/Helpers/NSRangeExtensions.swift @@ -33,7 +33,11 @@ public extension NSRange { } var lastCharacterRange: NSRange { - return NSRange(location: location + length, length: 1) + return NSRange(location: max(location + length - 1, 0), length: 1) + } + + var previousPosition: NSRange { + return NSRange(location: max(location - 1, 0), length: 0) } var nextPosition: NSRange { @@ -44,6 +48,14 @@ public extension NSRange { return location + length } + var nextCharacterRange: NSRange { + NSRange(location: location + length, length: 1) + } + + var previousCharacterRange: NSRange { + NSRange(location: location - 1, length: 1) + } + /// Converts the range to `UITextRange` in given `UITextInput`. Returns nil if the range is invalid in the `UITextInput`. /// - Parameter textInput: UITextInput to convert the range in. func toTextRange(textInput: UITextInput) -> UITextRange? {