From 1b1c35873a10dfec7340d65002bddafb9e62fe53 Mon Sep 17 00:00:00 2001 From: "Christiano, Kevin" Date: Thu, 11 Apr 2019 14:25:34 -0400 Subject: [PATCH] development of Label conversion. wip. --- MVMCoreUI/Atoms/Views/Label.swift | 42 ++++++++++++------------------- 1 file changed, 16 insertions(+), 26 deletions(-) diff --git a/MVMCoreUI/Atoms/Views/Label.swift b/MVMCoreUI/Atoms/Views/Label.swift index b0b32fcc..ee6ec1ab 100644 --- a/MVMCoreUI/Atoms/Views/Label.swift +++ b/MVMCoreUI/Atoms/Views/Label.swift @@ -22,7 +22,7 @@ class Label: UILabel, MVMCoreViewProtocol, MVMCoreUIMoleculeViewProtocol { // Set this to use a custom sizing object during updateView instead of the standard. var sizeObject: MFSizeObject? - private var scaleSize: NSNumber? + private var scaleSize: CGFloat? // Used for scaling the font in updateView. private var originalAttributedString: NSAttributedString? @@ -43,15 +43,9 @@ class Label: UILabel, MVMCoreViewProtocol, MVMCoreUIMoleculeViewProtocol { var hasText: Bool { - if let text = text, !text.isEmpty { - return true - } + guard let text = text, let attributedText = attributedText else { return false } - if let attributedText = attributedText, !attributedText.string.isEmpty { - return true - } - - return false + return !text.isEmpty || !attributedText.string.isEmpty } //------------------------------------------------------ @@ -60,9 +54,9 @@ class Label: UILabel, MVMCoreViewProtocol, MVMCoreUIMoleculeViewProtocol { func setupView() { - backgroundColor = UIColor.clear + backgroundColor = .clear numberOfLines = 0 - lineBreakMode = NSLineBreakMode.byWordWrapping + lineBreakMode = .byWordWrapping translatesAutoresizingMaskIntoConstraints = false } @@ -142,7 +136,6 @@ class Label: UILabel, MVMCoreViewProtocol, MVMCoreUIMoleculeViewProtocol { return Label(frame: CGRect.zero) } - //------------------------------------------------------ // MARK: - Functions //------------------------------------------------------ @@ -281,43 +274,40 @@ class Label: UILabel, MVMCoreViewProtocol, MVMCoreUIMoleculeViewProtocol { func updateView(_ size: CGFloat) { - scaleSize = NSNumber(value: Float(size)) + scaleSize = size if let originalAttributedString = 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? UIFont)?.withSize((MFStyler.sizeObjectGeneric(forCurrentDevice: ((value as? UIFont)?.pointSize)!)?.getValueBased(onSize: size))!) - attributedString.addAttribute(.font, value: font as Any, range: range) + if let fontObj = value as? UIFont, let stylerSize = MFStyler.sizeObjectGeneric(forCurrentDevice: fontObj.pointSize)?.getValueBased(onSize: size) { + + attributedString.addAttribute(.font, value: fontObj.withSize(stylerSize) as Any, range: range) + } }) attributedText = attributedString } else if !MVMCoreGetterUtility.fequal(a: Float(standardFontSize), b: 0.0) { - var sizeObject: MFSizeObject? = self.sizeObject - - if sizeObject == nil { - sizeObject = MFStyler.sizeObjectGeneric(forCurrentDevice: standardFontSize) + if let sizeObject: MFSizeObject = self.sizeObject ?? MFStyler.sizeObjectGeneric(forCurrentDevice: standardFontSize) { + self.font = self.font.withSize(sizeObject.getValueBased(onSize: size)) } - - self.font = self.font.withSize(sizeObject?.getValueBased(onSize: size) ?? 0.0) } } func setFont(_ font: UIFont, scale: Bool) { + self.font = font setScale(scale) } func setScale(_ scale: Bool) { + if scale { standardFontSize = font.pointSize - if let scaleSize = scaleSize { - updateView(CGFloat(scaleSize.floatValue)) - } else { - updateView(MVMCoreUISplitViewController.getApplicationViewWidth()) - } + updateView(scaleSize ?? MVMCoreUISplitViewController.getApplicationViewWidth()) } else { standardFontSize = 0 }