From ce68eafd348182bbb82ab231c9bb365e73b48f50 Mon Sep 17 00:00:00 2001 From: "Christiano, Kevin" Date: Wed, 3 Apr 2019 13:31:29 -0400 Subject: [PATCH] stopping for now. --- MVMCoreUI/Atoms/Views/Label.swift | 106 ++++++++++++++++++------------ 1 file changed, 65 insertions(+), 41 deletions(-) diff --git a/MVMCoreUI/Atoms/Views/Label.swift b/MVMCoreUI/Atoms/Views/Label.swift index fbff2265..88b07f00 100644 --- a/MVMCoreUI/Atoms/Views/Label.swift +++ b/MVMCoreUI/Atoms/Views/Label.swift @@ -59,6 +59,7 @@ class Label: UILabel, MVMCoreViewProtocol, MVMCoreUIMoleculeViewProtocol { //------------------------------------------------------ func setupView() { + backgroundColor = UIColor.clear numberOfLines = 0 lineBreakMode = NSLineBreakMode.byWordWrapping @@ -85,24 +86,32 @@ class Label: UILabel, MVMCoreViewProtocol, MVMCoreUIMoleculeViewProtocol { standardFontSize = size } + //------------------------------------------------------ + // MARK: - Functions + //------------------------------------------------------ + func updateView(_ size: CGFloat) { scaleSize = NSNumber(value: Float(size)) if let originalAttributedString = originalAttributedString { - var attributedString = NSMutableAttributedString(attributedString: originalAttributedString) + let attributedString = NSMutableAttributedString(attributedString: originalAttributedString) attributedString.removeAttribute(.font, range: NSRange(location: 0, length: attributedString.length)) + originalAttributedString.enumerateAttribute(.font, in: NSRange(location: 0, length: originalAttributedString.length), options: [], using: { value, range, stop in // Loop the original attributed string, resize the fonts. - let font: UIFont? = (value? as AnyObject).withSize(MFStyler.sizeObjectGeneric(forCurrentDevice: value?.pointSize).getValueBased(onSize: size)) - attributedString.addAttribute(.font, value: font, range: range) + let font: UIFont? = (value as? UIFont)?.withSize((MFStyler.sizeObjectGeneric(forCurrentDevice: ((value as? UIFont)?.pointSize)!)?.getValueBased(onSize: size))!) + attributedString.addAttribute(.font, value: font as Any, range: range) }) attributedText = attributedString - } else if !fequal(standardFontSize, 0) { + + } else if !MVMCoreGetterUtility.fequal(a: Float(standardFontSize), b: 0.0) { var sizeObject: MFSizeObject? = self.sizeObject + if sizeObject == nil { sizeObject = MFStyler.sizeObjectGeneric(forCurrentDevice: standardFontSize) } + self.font = self.font.withSize(sizeObject?.getValueBased(onSize: size) ?? 0.0) } } @@ -115,7 +124,11 @@ class Label: UILabel, MVMCoreViewProtocol, MVMCoreUIMoleculeViewProtocol { func setScale(_ scale: Bool) { if scale { standardFontSize = font.pointSize - updateView((scaleSize ? CGFloat(scaleSize?.floatValue ?? 0) : MVMCoreUISplitViewController.getApplicationViewWidth())) + if let scaleSize = scaleSize { + updateView(CGFloat(scaleSize.floatValue)) + } else { + updateView(MVMCoreUISplitViewController.getApplicationViewWidth()) + } } else { standardFontSize = 0 } @@ -148,7 +161,7 @@ class Label: UILabel, MVMCoreViewProtocol, MVMCoreUIMoleculeViewProtocol { case commonLabelB1(scale: Bool) case commonLabelTopAlert(scale: Bool) - func getLabel() -> Label { + func createLabel() -> Label { let label = Label() @@ -221,6 +234,10 @@ class Label: UILabel, MVMCoreViewProtocol, MVMCoreUIMoleculeViewProtocol { } } + //------------------------------------------------------ + // MARK: - Methods + //------------------------------------------------------ + func styleB2(_ scale: Bool) { MFStyler.styleLabelB2(self, genericScaling: false) setScale(scale) @@ -344,14 +361,14 @@ class Label: UILabel, MVMCoreViewProtocol, MVMCoreUIMoleculeViewProtocol { class func setLabel(_ label: UILabel?, withHTML html: String?) { let data: Data? = html?.data(using: .utf8) if data != nil { - var error: Error? = nil + let error: Error? = nil if let data = data { label?.attributedText = try? NSAttributedString(data: data, - options: [NSAttributedString.DocumentAttributeKey.documentType: NSAttributedString.DocumentType.html.rawValue], + 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")) + try? MVMCoreUILoggingHandler.shared()?.addError(toLog: MVMCoreErrorObject.createErrorObject(for: labelError, location: "LabelHTMLParse")!) } } } @@ -374,7 +391,7 @@ class Label: UILabel, MVMCoreViewProtocol, MVMCoreUIMoleculeViewProtocol { label.accessibilityLabel = accessibilityText } - var fontSize = json?.floatForKey("fontSize") + 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)) @@ -382,42 +399,41 @@ class Label: UILabel, MVMCoreViewProtocol, MVMCoreUIMoleculeViewProtocol { label.font = label.font.withSize(CGFloat(fontSize)) } - if let attributes = json?.arrayForKey("attributes") { - var attributedString = NSMutableAttributedString(string: label.text, attributes: [NSAttributedString.Key.font: label.font, + 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 { - var location = attribute.optionalNumber(forKey: "location") - var length = attribute.optionalNumber(forKey: "length") - if location && length { + 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) - var range = NSRange(location: Int(location.uintValue), length: Int(length.uintValue)) - var type = attribute.string(KeyType) + case "strikethrough": + attributedString.addAttribute(.strikethroughStyle, value: NSUnderlineStyle.thick, range: range) - if type == "underline" { - attributedString.addAttribute(.underlineStyle, value: NSUnderlineStyle.single, range: range) - - } else if type == "strikethrough" { - attributedString.addAttribute(.strikethroughStyle, value: NSUnderlineStyle.thick, range: range) - - } else if type == "color" { - if let colorHex = attribute.optionalStringForKey(KeyTextColor), !colorHex.isEmpty { - attributedString.addAttribute(.foregroundColor, value: UIColor.mfGet(forHex: colorHex), range: range) - } - } else if type == "font" { - - var 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, 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 @@ -425,6 +441,14 @@ class Label: UILabel, MVMCoreViewProtocol, MVMCoreUIMoleculeViewProtocol { } } + //------------------------------------------------------ + // MARK: - Atomization + //------------------------------------------------------ + + func setAsMolecule() { + // Empty + } + func setWithJSON(_ json: [AnyHashable: Any]?, delegate: NSObject?, additionalData: [AnyHashable: Any]?) { Label.setUILabel(self, withJSON: json, delegate: delegate, additionalData: additionalData) originalAttributedString = attributedText