From c1adbe2f2ee5abe7662eab1920cea1cde56aae97 Mon Sep 17 00:00:00 2001 From: "Khan, Arshad" Date: Fri, 25 Oct 2019 01:17:54 +0530 Subject: [PATCH] Crash fixes in Label update molecule name from progressBar to progressbar as per oneconfluence mapping name and Android mapping name. --- MVMCoreUI/Atoms/Views/Label.swift | 50 +++++++++++++++++-- .../MVMCoreUIMoleculeMappingObject.m | 2 +- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/MVMCoreUI/Atoms/Views/Label.swift b/MVMCoreUI/Atoms/Views/Label.swift index 6fc496bd..620b3caa 100644 --- a/MVMCoreUI/Atoms/Views/Label.swift +++ b/MVMCoreUI/Atoms/Views/Label.swift @@ -252,7 +252,7 @@ public typealias ActionBlock = () -> () let length = attribute["length"] as? Int else { continue } - let range = NSRange(location: location, length: length) + var range = NSRange(location: location, length: length) switch attributeType { case "underline": @@ -264,7 +264,17 @@ public typealias ActionBlock = () -> () case "color": if let colorHex = attribute.optionalStringForKey(KeyTextColor), !colorHex.isEmpty { - attributedString.removeAttribute(.foregroundColor, range: range) + // crash fix: removing attribute, even though it does not exists + let foregroundColorAttributesArray = attributedString.attributes(at: location, effectiveRange: &range).filter { (attribute) -> Bool in + if attribute.key == .foregroundColor { + return true + } else { + return false + } + } + if foregroundColorAttributesArray.isEmpty == false { + attributedString.removeAttribute(.foregroundColor, range: range) + } attributedString.addAttribute(.foregroundColor, value: UIColor.mfGet(forHex: colorHex), range: range) } case "image": @@ -286,8 +296,28 @@ public typealias ActionBlock = () -> () case "font": if let fontStyle = attribute.optionalStringForKey("style") { let styles = MFStyler.styleGetAttributedString("0", withStyle: fontStyle) - attributedString.removeAttribute(.font, range: range) - attributedString.removeAttribute(.foregroundColor, range: range) + // crash fix: removing font attribute, even though it does not exists + let fontAttributesArray = attributedString.attributes(at: location, effectiveRange: &range).filter { (attribute) -> Bool in + if attribute.key == .font { + return true + } else { + return false + } + } + if fontAttributesArray.isEmpty == false { + attributedString.removeAttribute(.font, range: range) + } + // crash fix: removing attribute, even though it does not exists + let foregroundColorAttributesArray = attributedString.attributes(at: location, effectiveRange: &range).filter { (attribute) -> Bool in + if attribute.key == .foregroundColor { + return true + } else { + return false + } + } + if foregroundColorAttributesArray.isEmpty == false { + attributedString.removeAttribute(.foregroundColor, range: range) + } attributedString.addAttributes(styles.attributes(at: 0, effectiveRange: nil), range: range) } else { let fontSize = attribute["size"] as? CGFloat @@ -300,7 +330,17 @@ public typealias ActionBlock = () -> () } if let font = font { - attributedString.removeAttribute(.font, range: range) + // crash fix: removing font attribute, even though it does not exists + let fontAttributesArray = attributedString.attributes(at: location, effectiveRange: &range).filter { (attribute) -> Bool in + if attribute.key == .font { + return true + } else { + return false + } + } + if fontAttributesArray.isEmpty == false { + attributedString.removeAttribute(.font, range: range) + } attributedString.addAttribute(.font, value: font, range: range) } } diff --git a/MVMCoreUI/OtherHandlers/MVMCoreUIMoleculeMappingObject.m b/MVMCoreUI/OtherHandlers/MVMCoreUIMoleculeMappingObject.m index bad7a224..c8679a56 100644 --- a/MVMCoreUI/OtherHandlers/MVMCoreUIMoleculeMappingObject.m +++ b/MVMCoreUI/OtherHandlers/MVMCoreUIMoleculeMappingObject.m @@ -41,7 +41,7 @@ @"checkbox" : Checkbox.class, @"checkboxWithLabelView" : CheckboxWithLabelView.class, @"cornerLabels" : CornerLabels.class, - @"progressBar": ProgressBar.class, + @"progressbar": ProgressBar.class, @"multiProgressBar": MultiProgress.class, @"listItem": MoleculeTableViewCell.class, @"accordionListItem": AccordionMoleculeTableViewCell.class,