From bebe5f6af514da3cd63246211723ab932090308f Mon Sep 17 00:00:00 2001 From: Diego Rey Mendez Date: Fri, 24 Nov 2017 09:15:41 -0300 Subject: [PATCH 1/3] WIP: trying a cleaner alternative for 811. --- Aztec/Classes/TextKit/TextStorage.swift | 8 +++----- Aztec/Classes/TextKit/TextView.swift | 8 +++++++- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/Aztec/Classes/TextKit/TextStorage.swift b/Aztec/Classes/TextKit/TextStorage.swift index 2596bfda0..1a10f0f91 100644 --- a/Aztec/Classes/TextKit/TextStorage.swift +++ b/Aztec/Classes/TextKit/TextStorage.swift @@ -267,18 +267,16 @@ open class TextStorage: NSTextStorage { edited([.editedAttributes, .editedCharacters], range: range, changeInLength: attrString.length - range.length) - let invalidateRange = NSMakeRange(range.location, attrString.length) - invalidateAttributes(in: invalidateRange) - endEditing() } override open func setAttributes(_ attrs: [AttributedStringKey: Any]?, range: NSRange) { beginEditing() - let fixedAttributes = ensureMatchingFontAndParagraphHeaderStyles(beforeApplying: attrs ?? [:], at: range) + //let fixedAttributes = ensureMatchingFontAndParagraphHeaderStyles(beforeApplying: attrs ?? [:], at: range) - textStore.setAttributes(fixedAttributes, range: range) + //textStore.setAttributes(fixedAttributes, range: range) + textStore.setAttributes(attrs, range: range) edited(.editedAttributes, range: range, changeInLength: 0) endEditing() diff --git a/Aztec/Classes/TextKit/TextView.swift b/Aztec/Classes/TextKit/TextView.swift index 4cedcffc5..131df44e8 100644 --- a/Aztec/Classes/TextKit/TextView.swift +++ b/Aztec/Classes/TextKit/TextView.swift @@ -248,7 +248,6 @@ open class TextView: UITextView { } } - /// Returns the collection of Typing Attributes, with all of the available 'String' keys properly converted into /// NSAttributedStringKey. Also known as: what you would expect from the SDK. /// @@ -599,6 +598,13 @@ open class TextView: UITextView { return caretRect } + + // This is only overridden due to iOS 11 issues since it's losing styles. + override open func unmarkText() { + preserveTypingAttributesForInsertion { + super.unmarkText() + } + } // MARK: - HTML Interaction From 80db44712e54069640168067eb0efd9d432f4cc2 Mon Sep 17 00:00:00 2001 From: Diego Rey Mendez Date: Fri, 24 Nov 2017 10:14:21 -0300 Subject: [PATCH 2/3] Fixes an issue with chinese characters. --- .../Formatters/Implementations/HeaderFormatter.swift | 11 +++++++---- Aztec/Classes/TextKit/TextStorage.swift | 7 +++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/Aztec/Classes/Formatters/Implementations/HeaderFormatter.swift b/Aztec/Classes/Formatters/Implementations/HeaderFormatter.swift index 176cfb17e..3d458020a 100644 --- a/Aztec/Classes/Formatters/Implementations/HeaderFormatter.swift +++ b/Aztec/Classes/Formatters/Implementations/HeaderFormatter.swift @@ -42,12 +42,15 @@ open class HeaderFormatter: ParagraphAttributeFormatter { } else { newParagraphStyle.replaceProperty(ofType: Header.self, with: header) } - - let targetFontSize = headerFontSize(for: headerLevel, defaultSize: defaultSize) + + let targetFontSize = CGFloat(headerFontSize(for: headerLevel, defaultSize: defaultSize)) var resultingAttributes = attributes + + let newDescriptor = font.fontDescriptor.addingAttributes([.size: targetFontSize]) + resultingAttributes[.paragraphStyle] = newParagraphStyle - resultingAttributes[.font] = font.withSize(CGFloat(targetFontSize)) - + resultingAttributes[.font] = UIFont(descriptor: newDescriptor, size: targetFontSize) + return resultingAttributes } diff --git a/Aztec/Classes/TextKit/TextStorage.swift b/Aztec/Classes/TextKit/TextStorage.swift index 1a10f0f91..d51244589 100644 --- a/Aztec/Classes/TextKit/TextStorage.swift +++ b/Aztec/Classes/TextKit/TextStorage.swift @@ -273,10 +273,9 @@ open class TextStorage: NSTextStorage { override open func setAttributes(_ attrs: [AttributedStringKey: Any]?, range: NSRange) { beginEditing() - //let fixedAttributes = ensureMatchingFontAndParagraphHeaderStyles(beforeApplying: attrs ?? [:], at: range) + let fixedAttributes = ensureMatchingFontAndParagraphHeaderStyles(beforeApplying: attrs ?? [:], at: range) - //textStore.setAttributes(fixedAttributes, range: range) - textStore.setAttributes(attrs, range: range) + textStore.setAttributes(fixedAttributes, range: range) edited(.editedAttributes, range: range, changeInLength: 0) endEditing() @@ -406,7 +405,7 @@ private extension TextStorage { guard oldLevel != newLevel else { return attrs } - + return fixFontAttribute(in: attrs, headerLevel: newLevel) } From da5078249233a7074bae23d62dfcf5df6759d923 Mon Sep 17 00:00:00 2001 From: Diego Rey Mendez Date: Fri, 24 Nov 2017 12:14:00 -0300 Subject: [PATCH 3/3] Clarifies a workaround. --- Aztec/Classes/TextKit/TextView.swift | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Aztec/Classes/TextKit/TextView.swift b/Aztec/Classes/TextKit/TextView.swift index 131df44e8..44e31ea5e 100644 --- a/Aztec/Classes/TextKit/TextView.swift +++ b/Aztec/Classes/TextKit/TextView.swift @@ -599,7 +599,14 @@ open class TextView: UITextView { return caretRect } - // This is only overridden due to iOS 11 issues since it's losing styles. + /// When typing with the Chinese keyboard, the text is automatically marked in the editor. + /// You have to press ENTER once to confirm your chosen input. The problem is that in iOS 11 + /// the typing attributes are lost when the text is unmarked, causing the font to be lost. + /// Since localized characters need specific fonts to be rendered, this causes some characters + /// to stop rendering completely. + /// + /// Reference: https://github.com/wordpress-mobile/AztecEditor-iOS/issues/811 + /// override open func unmarkText() { preserveTypingAttributesForInsertion { super.unmarkText()