From edde99cc23cbeee7cab9539849bbfa1713dd32ad Mon Sep 17 00:00:00 2001 From: "Christiano, Kevin" Date: Sun, 7 Apr 2019 20:18:40 -0400 Subject: [PATCH] Changes made to satisfy update to MFLabel. --- MVMCoreUI.xcodeproj/project.pbxproj | 4 +- MVMCoreUI/Atoms/Views/Label.swift | 491 +++++++++++----------------- 2 files changed, 186 insertions(+), 309 deletions(-) diff --git a/MVMCoreUI.xcodeproj/project.pbxproj b/MVMCoreUI.xcodeproj/project.pbxproj index 5d4a752c..5731bd78 100644 --- a/MVMCoreUI.xcodeproj/project.pbxproj +++ b/MVMCoreUI.xcodeproj/project.pbxproj @@ -157,10 +157,10 @@ D2C5001921F8ECDD001DA659 /* MVMCoreUIViewControllerMappingObject.m in Sources */ = {isa = PBXBuildFile; fileRef = D2C5001721F8ECDD001DA659 /* MVMCoreUIViewControllerMappingObject.m */; }; D2C5001D21F8EE67001DA659 /* LabelWithInternalButton.h in Headers */ = {isa = PBXBuildFile; fileRef = D2C5001B21F8EE66001DA659 /* LabelWithInternalButton.h */; settings = {ATTRIBUTES = (Public, ); }; }; D2C5001E21F8EE67001DA659 /* LabelWithInternalButton.m in Sources */ = {isa = PBXBuildFile; fileRef = D2C5001C21F8EE66001DA659 /* LabelWithInternalButton.m */; }; - DB891E832253FA8500022516 /* Label.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB891E822253FA8500022516 /* Label.swift */; }; DBC4391822442197001AB423 /* CaretView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBC4391622442196001AB423 /* CaretView.swift */; }; DBC4391922442197001AB423 /* DashLine.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBC4391722442197001AB423 /* DashLine.swift */; }; DBC4391B224421A0001AB423 /* CaretButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBC4391A224421A0001AB423 /* CaretButton.swift */; }; + DBEFFA04225A829700230692 /* Label.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB891E822253FA8500022516 /* Label.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -886,6 +886,7 @@ D29DF25421E6A177003B2FB9 /* MFMdnTextField.m in Sources */, D282AABA224131D100C46919 /* MFTransparentGIFView.swift in Sources */, D2A514672213885800345BFB /* StandardHeaderView.swift in Sources */, + DBEFFA04225A829700230692 /* Label.swift in Sources */, D29DF13021E6851E003B2FB9 /* MVMCoreUITopAlertShortView.m in Sources */, D28B4F8B21FF967C00712C7A /* MVMCoreUIObject.m in Sources */, D29DF26D21E6AA0B003B2FB9 /* FLAnimatedImageView.m in Sources */, @@ -893,7 +894,6 @@ D282AACB2243C61700C46919 /* ButtonView.swift in Sources */, 0105618F224BBE7700E1557D /* FormValidator+FormParams.swift in Sources */, D29DF2AE21E7B3A4003B2FB9 /* MFTextView.m in Sources */, - DB891E832253FA8500022516 /* Label.swift in Sources */, D29DF18121E69E50003B2FB9 /* MFView.m in Sources */, D29DF18321E69E54003B2FB9 /* SeparatorView.m in Sources */, D29DF17A21E69E1F003B2FB9 /* MFCustomButton.m in Sources */, diff --git a/MVMCoreUI/Atoms/Views/Label.swift b/MVMCoreUI/Atoms/Views/Label.swift index 88b07f00..61bf9a12 100644 --- a/MVMCoreUI/Atoms/Views/Label.swift +++ b/MVMCoreUI/Atoms/Views/Label.swift @@ -54,6 +54,7 @@ class Label: UILabel, MVMCoreViewProtocol, MVMCoreUIMoleculeViewProtocol { return false } + //------------------------------------------------------ // MARK: - Initialization //------------------------------------------------------ @@ -86,10 +87,193 @@ class Label: UILabel, MVMCoreViewProtocol, MVMCoreUIMoleculeViewProtocol { standardFontSize = size } + //------------------------------------------------------ + // MARK: - Enum + //------------------------------------------------------ + + enum Style { + case commonLabelB2(scale: Bool) + case commonLabelH1(scale: Bool) + case commonLabelH2(scale: Bool) + case commonLabelH32(scale: Bool) + case commonLabelH3(scale: Bool) + case commonLabelB3(scale: Bool) + case commonLabelB20(scale: Bool) + case commonLabelB1(scale: Bool) + + func createLabel() -> Label { + + let label = Label() + + switch self { + case .commonLabelB2(let scale): + label.styleB2(scale) + + case .commonLabelH1(let scale): + label.styleH1(scale) + + case .commonLabelH2(let scale): + label.styleH2(scale) + + case .commonLabelH32(let scale): + label.styleH32(scale) + + case .commonLabelH3(let scale): + label.styleH3(scale) + + case .commonLabelB3(let scale): + label.styleB3(scale) + + case .commonLabelB20(let scale): + label.styleB20(scale) + + case .commonLabelB1(let scale): + label.styleB1(scale) + } + + return label + } + } + //------------------------------------------------------ // MARK: - Functions //------------------------------------------------------ + static func label() -> Label { + return Label(frame: CGRect.zero) + } + + static func setLabel(_ label: UILabel?, withHTML html: String?) { + let data: Data? = html?.data(using: .utf8) + if data != nil { + let error: Error? = nil + if let data = data { + label?.attributedText = try? NSAttributedString(data: data, + options: [NSAttributedString.DocumentReadingOptionKey.documentType:NSAttributedString.DocumentType.html, NSAttributedString.DocumentReadingOptionKey.characterEncoding: String.Encoding.utf8.rawValue], + documentAttributes: nil) + + // label?.attributedText = try? NSAttributedString(data: data, options: [ + // NSAttributedString.DocumentAttributeKey.documentType: NSAttributedString.DocumentType.html.rawValue + // ], documentAttributes: nil) + } + if let labelError = error { + try? MVMCoreUILoggingHandler.shared()?.addError(toLog: MVMCoreErrorObject.createErrorObject(for: labelError, location: "LabelHTMLParse")!) + } + } + } + + static func setUILabel(_ label: UILabel?, withJSON json: [AnyHashable: Any]?, delegate: NSObject?, additionalData: [AnyHashable: Any]?) { + + if let label = label { + label.text = json?.optionalStringForKey(KeyText) + setLabel(label, withHTML: json?.stringForkey("html")) + + if let textColorHex = json?.optionalStringForKey(KeyTextColor) { + label.textColor = UIColor.mfGet(forHex: textColorHex) + } + + if let backgroundColorHex = json?.optionalStringForKey(KeyBackgroundColor) { + label.backgroundColor = UIColor.mfGet(forHex: backgroundColorHex) + } + + if let accessibilityText = json?.optionalStringForKey("accessibilityText") { + label.accessibilityLabel = accessibilityText + } + + let fontSize = json?.floatForKey("fontSize") + + if let fontName = json?.optionalStringForKey("fontName"), let fontSize = fontSize { + label.font = MFFonts.mfFont(withName: fontName, size: CGFloat(fontSize) == 0.0 ? label.font.pointSize : CGFloat(fontSize)) + } else if let fontSize = fontSize { + label.font = label.font.withSize(CGFloat(fontSize)) + } + + if let attributes = json?.arrayForKey("attributes"), let labelText = label.text { + let attributedString = NSMutableAttributedString(string: labelText, attributes: [NSAttributedString.Key.font: label.font, + NSAttributedString.Key.foregroundColor: label.textColor]) + for case let attribute as [String: Any] in attributes { + let location = attribute.floatForKey("location") + let length = attribute.floatForKey("length") + + let range = NSRange(location: Int(location), length: Int(length)) + + switch attribute.stringForkey(KeyType){ + case "underline": + attributedString.addAttribute(.underlineStyle, value: NSUnderlineStyle.single, range: range) + + case "strikethrough": + attributedString.addAttribute(.strikethroughStyle, value: NSUnderlineStyle.thick, range: range) + + case "color": + if let colorHex = attribute.optionalStringForKey(KeyTextColor), !colorHex.isEmpty { + attributedString.addAttribute(.foregroundColor, value: UIColor.mfGet(forHex: colorHex), range: range) + } + case "font": + let fontSize = attribute.floatForKey("size") + var font: UIFont? + + if let fontName = attribute.optionalStringForKey("name"), !fontName.isEmpty { + font = MFFonts.mfFont(withName: fontName, size: fontSize != 0.0 ? CGFloat(fontSize) : CGFloat(label.font.pointSize)) + } else { + font = label.font.withSize(CGFloat(fontSize)) + } + + if font != nil { + attributedString.addAttribute(.font, value: font as Any, range: range) + } + default: + continue + } + } + label.attributedText = attributedString + } + } + } + + //------------------------------------------------------ + // MARK: - Methods + //------------------------------------------------------ + + func styleH1(_ scale: Bool) { + MFStyler.styleLabelH1(self, genericScaling: false) + setScale(scale) + } + + func styleH2(_ scale: Bool) { + MFStyler.styleLabelH2(self, genericScaling: false) + setScale(scale) + } + + func styleH3(_ scale: Bool) { + MFStyler.styleLabelH3(self, genericScaling: false) + setScale(scale) + } + + func styleH32(_ scale: Bool) { + MFStyler.styleLabelH32(self, genericScaling: false) + setScale(scale) + } + + func styleB1(_ scale: Bool) { + MFStyler.styleLabelB1(self, genericScaling: false) + setScale(scale) + } + + func styleB2(_ scale: Bool) { + MFStyler.styleLabelB2(self, genericScaling: false) + setScale(scale) + } + + func styleB3(_ scale: Bool) { + MFStyler.styleLabelB3(self, genericScaling: false) + setScale(scale) + } + + func styleB20(_ scale: Bool) { + MFStyler.styleLabelB20(self, genericScaling: false) + setScale(scale) + } + func updateView(_ size: CGFloat) { scaleSize = NSNumber(value: Float(size)) @@ -134,313 +318,6 @@ class Label: UILabel, MVMCoreViewProtocol, MVMCoreUIMoleculeViewProtocol { } } - class func label() -> Label { - return Label(frame: CGRect.zero) - } - - enum Style { - case commonLabelB2(scale: Bool) - case commonLabelBody(size: CGFloat, scale: Bool) - case commonLabelBodyBold(scale: Bool) - case commonLabelBodyLighter(scale: Bool) - case commonLabelBodyLarge(scale: Bool) - case commonLabelH1(scale: Bool) - case commonLabelH2(scale: Bool) - case commonLabelHeadlineSmall(scale: Bool) - case commonLabelHeadline(size: CGFloat, scale: Bool) - case commonLabelHeadlineBlack(size: CGFloat, scale: Bool) - case commonLabelH3(scale: Bool) - case commonLabelSubheadBold(scale: Bool) - case commonLabelSubheadBoldLarge(scale: Bool) - case commonLabelB3(scale: Bool) - case commonLabelFeedMessage(scale: Bool) - case commonLabelFeedSubMessage(scale: Bool) - case commonLabelFeedHeadline(scale: Bool) - case commonLabelFeedTitle(scale: Bool) - case commonLabelPlanCardTitle(scale: Bool) - case commonLabelB1(scale: Bool) - case commonLabelTopAlert(scale: Bool) - - func createLabel() -> Label { - - let label = Label() - - switch self { - case .commonLabelB2(let scale): - label.styleB2(scale) - - case .commonLabelBody(let size, let scale): - label.styleBody(withSize: size, scale: scale) - - case .commonLabelBodyBold(let scale): - label.styleBodyBold(scale) - - case .commonLabelBodyLighter(let scale): - label.styleBodyLighter(scale) - - case .commonLabelBodyLarge(let scale): - label.styleBodyLarge(scale) - - case .commonLabelH1(let scale): - label.styleH1(scale) - - case .commonLabelH2(let scale): - label.styleH2(scale) - - case .commonLabelHeadlineSmall(let scale): - label.styleHeadlineSmall(scale) - - case .commonLabelHeadline(let size, let scale): - label.styleHeadline(withSize: size, scale: scale) - - case .commonLabelHeadlineBlack(let size, let scale): - label.styleHeadlineBlack(withSize: size, scale: scale) - - case .commonLabelH3(let scale): - label.styleH3(scale) - - case .commonLabelSubheadBold(let scale): - label.styleSubheadBold(scale) - - case .commonLabelSubheadBoldLarge(let scale): - label.styleSubheadBoldLarge(scale) - - case .commonLabelB3(let scale): - label.styleB3(scale) - - case .commonLabelFeedMessage(let scale): - label.styleFeedMessage(scale) - - case .commonLabelFeedSubMessage(let scale): - label.styleFeedSubMessage(scale) - - case .commonLabelFeedHeadline(let scale): - label.styleFeedHeadline(scale) - - case .commonLabelFeedTitle(let scale): - label.styleFeedTitle(scale) - - case .commonLabelPlanCardTitle(let scale): - label.stylePlanCardTitle(scale) - - case .commonLabelB1(let scale): - label.styleB1(scale) - - case .commonLabelTopAlert(let scale): - label.styleTopAlert(scale) - } - - return label - } - } - - //------------------------------------------------------ - // MARK: - Methods - //------------------------------------------------------ - - func styleB2(_ scale: Bool) { - MFStyler.styleLabelB2(self, genericScaling: false) - setScale(scale) - } - - func styleBody(withSize size: CGFloat, scale: Bool) { - MFStyler.styleLabelB2(self, size: size, genericScaling: false) - setScale(scale) - } - - func styleBodyBold(_ scale: Bool) { - MFStyler.styleLabelBodyBold(self, genericScaling: false) - setScale(scale) - } - - func styleBodyLighter(_ scale: Bool) { - MFStyler.styleLabelBodyLighter(self, genericScaling: false) - setScale(scale) - } - - func styleBodyLarge(_ scale: Bool) { - MFStyler.styleLabelBodyLarge(self, genericScaling: false) - setScale(scale) - } - - func styleH1(_ scale: Bool) { - MFStyler.styleLabelH1(self, genericScaling: false) - setScale(scale) - } - - func styleH2(_ scale: Bool) { - MFStyler.styleLabelH2(self, genericScaling: false) - setScale(scale) - } - - func styleHeadlineLarge(_ scale: Bool) { - MFStyler.styleLabelHeadlineLarge(self, genericScaling: false) - setScale(scale) - } - - func styleHeadlineSmall(_ scale: Bool) { - MFStyler.styleLabelHeadlineSmall(self, genericScaling: false) - setScale(scale) - } - - func styleHeadline(withSize size: CGFloat, scale: Bool) { - MFStyler.styleLabelH2(self, size: size, genericScaling: false) - setScale(scale) - } - - func styleHeadlineBlack(withSize size: CGFloat, scale: Bool) { - MFStyler.styleLabelHeadlineBlack(self, size: size, genericScaling: false) - setScale(scale) - } - - func styleH3(_ scale: Bool) { - MFStyler.styleLabelH3(self, genericScaling: false) - setScale(scale) - } - - func styleSubheadBold(_ scale: Bool) { - MFStyler.styleLabelSubheadBold(self, genericScaling: false) - setScale(scale) - } - - func styleSubheadBoldLarge(_ scale: Bool) { - MFStyler.styleLabelSubheadBoldLarge(self, genericScaling: false) - setScale(scale) - } - - func styleB3(_ scale: Bool) { - MFStyler.styleLabelB3(self, genericScaling: false) - setScale(scale) - } - - func styleFeedMessage(_ scale: Bool) { - MFStyler.styleLabelFeedMessage(self, genericScaling: false) - setScale(scale) - } - - func styleFeedSubMessage(_ scale: Bool) { - MFStyler.styleLabelFeedSubMessage(self, genericScaling: false) - setScale(scale) - } - - func styleFeedHeadline(_ scale: Bool) { - MFStyler.styleLabelHeadlineFeed(self, genericScaling: false) - setScale(scale) - } - - func styleFeedTitle(_ scale: Bool) { - MFStyler.styleFeedCardTitleLabel(self, genericScaling: false) - setScale(scale) - } - - func stylePlanCardTitle(_ scale: Bool) { - font = MFStyler.font(forPlanCardTitle: false) - setScale(scale) - } - - func styleB1(_ scale: Bool) { - MFStyler.styleLabelB1(self, genericScaling: false) - setScale(scale) - } - - func styleTableRow(_ scale: Bool) { - MFStyler.styleLabelB1(self, genericScaling: false) - setScale(scale) - } - - func styleTopAlert(_ scale: Bool) { - font = MFStyler.fontB1(false) - setScale(scale) - } - - func setFontH1(_ scale: Bool) { - font = MFStyler.fontH1(false) - setScale(scale) - } - - class func setLabel(_ label: UILabel?, withHTML html: String?) { - let data: Data? = html?.data(using: .utf8) - if data != nil { - let error: Error? = nil - if let data = data { - label?.attributedText = try? NSAttributedString(data: data, - options: [NSAttributedString.DocumentReadingOptionKey.documentType:NSAttributedString.DocumentType.html, NSAttributedString.DocumentReadingOptionKey.characterEncoding: String.Encoding.utf8.rawValue], - documentAttributes: nil) - } - if let labelError = error { - try? MVMCoreUILoggingHandler.shared()?.addError(toLog: MVMCoreErrorObject.createErrorObject(for: labelError, location: "LabelHTMLParse")!) - } - } - } - - class func setUILabel(_ label: UILabel?, withJSON json: [AnyHashable: Any]?, delegate: NSObject?, additionalData: [AnyHashable: Any]?) { - - if let label = label { - label.text = json?.optionalStringForKey(KeyText) - setLabel(label, withHTML: json?.stringForkey("html")) - - if let textColorHex = json?.optionalStringForKey(KeyTextColor) { - label.textColor = UIColor.mfGet(forHex: textColorHex) - } - - if let backgroundColorHex = json?.optionalStringForKey(KeyBackgroundColor) { - label.backgroundColor = UIColor.mfGet(forHex: backgroundColorHex) - } - - if let accessibilityText = json?.optionalStringForKey("accessibilityText") { - label.accessibilityLabel = accessibilityText - } - - let fontSize = json?.floatForKey("fontSize") - - if let fontName = json?.optionalStringForKey("fontName"), let fontSize = fontSize { - label.font = MFFonts.mfFont(withName: fontName, size: CGFloat(fontSize) == 0.0 ? label.font.pointSize : CGFloat(fontSize)) - } else if let fontSize = fontSize { - label.font = label.font.withSize(CGFloat(fontSize)) - } - - if let attributes = json?.arrayForKey("attributes"), let labelText = label.text { - let attributedString = NSMutableAttributedString(string: labelText, attributes: [NSAttributedString.Key.font: label.font, - NSAttributedString.Key.foregroundColor: label.textColor]) - for case let attribute as [String: Any] in attributes { - let location = attribute.floatForKey("location") - let length = attribute.floatForKey("length") - - let range = NSRange(location: Int(location), length: Int(length)) - - switch attribute.stringForkey(KeyType){ - case "underline": - attributedString.addAttribute(.underlineStyle, value: NSUnderlineStyle.single, range: range) - - case "strikethrough": - attributedString.addAttribute(.strikethroughStyle, value: NSUnderlineStyle.thick, range: range) - - case "color": - if let colorHex = attribute.optionalStringForKey(KeyTextColor), !colorHex.isEmpty { - attributedString.addAttribute(.foregroundColor, value: UIColor.mfGet(forHex: colorHex), range: range) - } - case "font": - let fontSize = attribute.floatForKey("size") - var font: UIFont? - - if let fontName = attribute.optionalStringForKey("name"), !fontName.isEmpty { - font = MFFonts.mfFont(withName: fontName, size: fontSize != 0.0 ? CGFloat(fontSize) : CGFloat(label.font.pointSize)) - } else { - font = label.font.withSize(CGFloat(fontSize)) - } - - if font != nil { - attributedString.addAttribute(.font, value: font as Any, range: range) - } - default: - continue - } - } - label.attributedText = attributedString - } - } - } - //------------------------------------------------------ // MARK: - Atomization //------------------------------------------------------