diff --git a/Aztec/Classes/EditorView/EditorView.swift b/Aztec/Classes/EditorView/EditorView.swift index 12eb0530c..3128cee57 100644 --- a/Aztec/Classes/EditorView/EditorView.swift +++ b/Aztec/Classes/EditorView/EditorView.swift @@ -6,12 +6,18 @@ import UIKit public class EditorView: UIView { public let htmlTextView: UITextView public let richTextView: TextView + public var htmlStorage: HTMLStorage { + guard let htmlStorage = htmlTextView.textStorage as? HTMLStorage else { + fatalError("If this happens, something is very off on the init config") + } + return htmlStorage + } // MARK: - Encoding / Decoding static let htmlTextViewKey = "Aztec.EditorView.htmlTextView" static let richTextViewKey = "Aztec.EditorView.richTextView" - + // MARK: - Content Insets public var contentInset: UIEdgeInsets { @@ -117,10 +123,10 @@ public class EditorView: UIView { let storage = HTMLStorage(defaultFont: defaultHTMLFont) let layoutManager = NSLayoutManager() let container = NSTextContainer() - + storage.addLayoutManager(layoutManager) layoutManager.addTextContainer(container) - + self.htmlTextView = UITextView(frame: .zero, textContainer: container) self.richTextView = TextView(defaultFont: defaultFont, defaultParagraphStyle: defaultParagraphStyle, defaultMissingImage: defaultMissingImage) diff --git a/Aztec/Classes/TextKit/HTMLStorage.swift b/Aztec/Classes/TextKit/HTMLStorage.swift index 17e2e1eae..09873f8a8 100644 --- a/Aztec/Classes/TextKit/HTMLStorage.swift +++ b/Aztec/Classes/TextKit/HTMLStorage.swift @@ -15,6 +15,10 @@ open class HTMLStorage: NSTextStorage { /// open var font: UIFont + /// Color to be applied over HTML text + /// + open var textColor = Styles.defaultTextColor + /// Color to be applied over HTML Comments /// open var commentColor = Styles.defaultCommentColor @@ -124,7 +128,7 @@ private extension HTMLStorage { func colorizeHTML() { let fullStringRange = rangeOfEntireString - removeAttribute(.foregroundColor, range: fullStringRange) + addAttribute(.foregroundColor, value: textColor, range: fullStringRange) addAttribute(.font, value: font, range: fullStringRange) let tags = RegExes.html.matches(in: string, options: [], range: fullStringRange) @@ -164,6 +168,7 @@ extension HTMLStorage { /// Default Styles /// public struct Styles { + static let defaultTextColor = UIColor.black static let defaultCommentColor = UIColor.lightGray static let defaultTagColor = UIColor(red: 0x00/255.0, green: 0x75/255.0, blue: 0xB6/255.0, alpha: 0xFF/255.0) static let defaultQuotedColor = UIColor(red: 0x6E/255.0, green: 0x96/255.0, blue: 0xB1/255.0, alpha: 0xFF/255.0) diff --git a/AztecTests/EditorView/EditorViewTests.swift b/AztecTests/EditorView/EditorViewTests.swift index e11ba1986..57a1e87a2 100644 --- a/AztecTests/EditorView/EditorViewTests.swift +++ b/AztecTests/EditorView/EditorViewTests.swift @@ -80,4 +80,24 @@ class EditorViewTests: XCTestCase { XCTAssertEqual(editorView.editingMode, .richText) XCTAssertEqual(editorView.activeView, editorView.richTextView) } + + func testHTMLStorageTextColor() { + let font = UIFont.systemFont(ofSize: 14) + let editorView = Aztec.EditorView( + defaultFont: font, + defaultHTMLFont: font, + defaultParagraphStyle: ParagraphStyle(), + defaultMissingImage: UIImage()) + + XCTAssertEqual(editorView.htmlStorage.textColor, HTMLStorage.Styles.defaultTextColor) + + editorView.htmlStorage.textColor = .red + editorView.richTextView.text = "Hello World" + editorView.toggleEditingMode() + + let textColor = editorView.htmlStorage.attribute(.foregroundColor, at: 3, effectiveRange: nil) as! UIColor + + XCTAssertEqual(editorView.htmlStorage.textColor, UIColor.red) + XCTAssertEqual(textColor, UIColor.red) + } } diff --git a/AztecTests/TextKit/HTMLStorageTests.swift b/AztecTests/TextKit/HTMLStorageTests.swift index a42778690..4b86d724e 100644 --- a/AztecTests/TextKit/HTMLStorageTests.swift +++ b/AztecTests/TextKit/HTMLStorageTests.swift @@ -63,4 +63,22 @@ class HTMLStorageTests: XCTestCase { XCTAssertEqual(openingTagColor, HTMLStorage.Styles.defaultTagColor) XCTAssertEqual(closingTagColor, HTMLStorage.Styles.defaultTagColor) } + + func testSetTextColor() { + let font = UIFont.boldSystemFont(ofSize: 12) + let storage = HTMLStorage(defaultFont: font) + let initialString = "Hello there" + + storage.insert(NSAttributedString(string: initialString), at: 0) + + XCTAssertEqual(storage.string, initialString) + XCTAssertEqual(storage.textColor, HTMLStorage.Styles.defaultTextColor) + + storage.textColor = .red + storage.replaceCharacters(in: NSRange(location: 0, length: 4), with: NSAttributedString(string: "Hello world")) + + let textColor = storage.attribute(.foregroundColor, at: 0, effectiveRange: nil) as! UIColor + + XCTAssertEqual(textColor, UIColor.red) + } } diff --git a/WordPress-Aztec-iOS.podspec b/WordPress-Aztec-iOS.podspec index 0be21edde..1701bf162 100644 --- a/WordPress-Aztec-iOS.podspec +++ b/WordPress-Aztec-iOS.podspec @@ -8,7 +8,7 @@ Pod::Spec.new do |s| s.name = 'WordPress-Aztec-iOS' - s.version = '1.8.1' + s.version = '1.9.0-beta.1' s.summary = 'The native HTML Editor.' # This description is used to generate tags and improve search results. diff --git a/WordPress-Editor-iOS.podspec b/WordPress-Editor-iOS.podspec index f7e6bb17c..b10d640ff 100644 --- a/WordPress-Editor-iOS.podspec +++ b/WordPress-Editor-iOS.podspec @@ -8,7 +8,7 @@ Pod::Spec.new do |s| s.name = 'WordPress-Editor-iOS' - s.version = '1.8.1' + s.version = '1.9.0-beta.1' s.summary = 'The WordPress HTML Editor.' # This description is used to generate tags and improve search results.