diff --git a/Aztec/Classes/TextKit/TextView.swift b/Aztec/Classes/TextKit/TextView.swift index 8a7b4b3ab..60a532355 100644 --- a/Aztec/Classes/TextKit/TextView.swift +++ b/Aztec/Classes/TextKit/TextView.swift @@ -457,7 +457,9 @@ open class TextView: UITextView { ensureRemovalOfParagraphStylesBeforeRemovingCharacter(at: selectedRange) - super.deleteBackward() + preserveTypingAttributes { + super.deleteBackward() + } ensureRemovalOfParagraphAttributesWhenPressingBackspaceAndEmptyingTheDocument() ensureCursorRedraw(afterEditing: deletedString.string) @@ -967,6 +969,28 @@ open class TextView: UITextView { } + // WORKAROUND: iOS 11 introduced an issue that's causing UITextView to lose it's typing + // attributes under certain circumstances. This method will determine the Typing Attributes based on + /// the TextStorage attributes, whenever possible. + /// + /// Issue: https://github.com/wordpress-mobile/AztecEditor-iOS/issues/749 + /// + private func preserveTypingAttributes(beforeDeletion block: () -> Void) { + let document = textStorage.string + guard selectedRange.location == document.characters.count else { + block() + return + } + + let previousLocation = max(selectedRange.location - 1, 0) + let previousAttributes = textStorage.attributes(at: previousLocation, effectiveRange: nil) + + block() + + typingAttributes = previousAttributes + } + + // MARK: - Links /// Adds a link to the designated url on the specified range. diff --git a/AztecTests/TextKit/TextViewTests.swift b/AztecTests/TextKit/TextViewTests.swift index 77b1b6afc..5270e6deb 100644 --- a/AztecTests/TextKit/TextViewTests.swift +++ b/AztecTests/TextKit/TextViewTests.swift @@ -1409,6 +1409,9 @@ class TextViewTests: XCTestCase { XCTAssertEqual(textView.text, Constants.sampleText0 + Constants.sampleText1 + String(.lineFeed) + String(.paragraphSeparator)) } + + // MARK: - Media + func testInsertVideo() { let textView = createEmptyTextView() let _ = textView.replaceWithVideo(at: NSRange(location:0, length:0), sourceURL: URL(string: "video.mp4")!, posterURL: URL(string: "video.jpg"), placeHolderImage: nil) @@ -1447,6 +1450,9 @@ class TextViewTests: XCTestCase { XCTAssertEqual(textView.getHTML(), "
") } + + // MARK: - Comments + /// This test check if the insertion of a Comment Attachment works correctly and the expected tag gets inserted /// func testInsertComment() { @@ -1470,6 +1476,9 @@ class TextViewTests: XCTestCase { XCTAssertEqual(html, "") } + + // MARK: - HR + /// This test check if the insertion of an horizontal ruler works correctly and the hr tag is inserted /// func testReplaceRangeWithHorizontalRuler() { @@ -1571,6 +1580,9 @@ class TextViewTests: XCTestCase { XCTAssertEqual(textView.getHTML(), "
First Item