Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions Aztec/Classes/EditorView/EditorView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)

Expand Down
7 changes: 6 additions & 1 deletion Aztec/Classes/TextKit/HTMLStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
20 changes: 20 additions & 0 deletions AztecTests/EditorView/EditorViewTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
18 changes: 18 additions & 0 deletions AztecTests/TextKit/HTMLStorageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,22 @@ class HTMLStorageTests: XCTestCase {
XCTAssertEqual(openingTagColor, HTMLStorage.Styles.defaultTagColor)
XCTAssertEqual(closingTagColor, HTMLStorage.Styles.defaultTagColor)
}

func testSetTextColor() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the tests! 🙏

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)
}
}
2 changes: 1 addition & 1 deletion WordPress-Aztec-iOS.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion WordPress-Editor-iOS.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down