Skip to content

Commit d4cd119

Browse files
committed
Fix for paragraph style changes with mixed character styles.
This code to update the paragraph attribute removals in a way that all the paragraph is changed when there is mixed styles in the paragraph.
1 parent bbe9abd commit d4cd119

3 files changed

Lines changed: 39 additions & 8 deletions

File tree

Aztec/Classes/Formatters/Base/ParagraphAttributeFormatter.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ extension ParagraphAttributeFormatter {
3737
let rangeToApply = applicationRange(for: range, in: text)
3838

3939
text.replaceOcurrences(of: String(.paragraphSeparator), with: String(.lineFeed), within: rangeToApply)
40-
41-
text.enumerateAttributes(in: rangeToApply, options: []) { (attributes, range, stop) in
42-
let currentAttributes = text.attributes(at: range.location, effectiveRange: nil)
40+
// We copy the string that gets update to avoid that removal of paragraph attributes, like NSParagraphStyle,
41+
// on a certain range affect all the paragraph and have side effects on the remaining removal
42+
let updatedText = text.mutableCopy() as! NSMutableAttributedString
43+
text.enumerateAttributes(in: rangeToApply, options: []) { (currentAttributes, range, stop) in
4344
let attributes = remove(from: currentAttributes)
44-
45-
text.setAttributes(attributes, range: range)
45+
updatedText.setAttributes(attributes, range: range)
4646
}
47-
47+
text.replaceCharacters(in: rangeToApply, with: updatedText.attributedSubstring(from: rangeToApply))
4848
return rangeToApply
4949
}
5050
}

Aztec/Classes/Formatters/Implementations/HeaderFormatter.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,12 @@ open class HeaderFormatter: ParagraphAttributeFormatter {
7070
if let font = attributes[.font] as? UIFont {
7171
var newFont = font.withSize(CGFloat(header.defaultFontSize))
7272
if Configuration.headersWithBoldTrait {
73-
newFont = newFont.modifyTraits(.traitBold, enable: false)
73+
newFont = newFont.modifyTraits(.traitBold, enable: false)
74+
if attributes[.shadow] != nil {
75+
resultingAttributes.removeValue(forKey: .shadow)
76+
resultingAttributes.removeValue(forKey: .kern)
77+
newFont = newFont.modifyTraits(.traitBold, enable: true)
78+
}
7479
}
7580
resultingAttributes[.font] = newFont
7681
}
@@ -82,7 +87,9 @@ open class HeaderFormatter: ParagraphAttributeFormatter {
8287
guard let paragraphStyle = attributes[.paragraphStyle] as? ParagraphStyle else {
8388
return false
8489
}
85-
90+
if headerLevel == .none {
91+
return paragraphStyle.headerLevel != 0
92+
}
8693
return paragraphStyle.headerLevel != 0 && paragraphStyle.headerLevel == headerLevel.rawValue
8794
}
8895
}

AztecTests/TextKit/TextStorageTests.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,4 +538,28 @@ class TextStorageTests: XCTestCase {
538538

539539
XCTAssertEqual(expectedResult, result)
540540
}
541+
542+
/// Verifies that the all header formatting is removed when Header as styles (bold or italic)
543+
///
544+
func testAllHeaderFormattingIsRemoved() {
545+
let initialHTML = "<p>Hello World</p>"
546+
547+
// Setup
548+
let defaultAttributes: [NSAttributedString.Key: Any] = [.font: UIFont.systemFont(ofSize: 14),
549+
.paragraphStyle: ParagraphStyle.default]
550+
storage.setHTML(initialHTML, defaultAttributes: defaultAttributes)
551+
552+
let formatterH1 = HeaderFormatter(headerLevel: .h1)
553+
let formatterBold = BoldWithShadowForHeadingFormatter()
554+
555+
formatterH1.applyAttributes(to: storage, at: NSRange(location:0, length: 0))
556+
formatterBold.applyAttributes(to: storage, at: NSRange(location: 6, length: 3))
557+
558+
formatterH1.removeAttributes(from: storage, at: NSRange(location:0, length: 0))
559+
560+
storage.enumerateAttributes(in: storage.rangeOfEntireString, options: []) { (attributes, range, stop) in
561+
let font = attributes[.font] as! UIFont
562+
XCTAssert(font.pointSize == 14)
563+
}
564+
}
541565
}

0 commit comments

Comments
 (0)