Converted MFLabel to Label.

This commit is contained in:
Christiano, Kevin 2019-04-12 12:12:33 -04:00
parent 1b1c35873a
commit d6fdf9958e

View File

@ -33,7 +33,7 @@ class Label: UILabel, MVMCoreViewProtocol, MVMCoreUIMoleculeViewProtocol {
} }
set(accessibilityTraits) { set(accessibilityTraits) {
if accessibilityTraits.rawValue & UIAccessibilityTraits.header.rawValue != 0 { if accessibilityTraits.rawValue & UIAccessibilityTraits.header.rawValue != 0 {
let noHeaderTraits: UIAccessibilityTraits = UIAccessibilityTraits(rawValue: accessibilityTraits.rawValue - UIAccessibilityTraits.header.rawValue) let noHeaderTraits = UIAccessibilityTraits(rawValue: accessibilityTraits.rawValue - UIAccessibilityTraits.header.rawValue)
super.accessibilityTraits = noHeaderTraits super.accessibilityTraits = noHeaderTraits
} else { } else {
super.accessibilityTraits = accessibilityTraits super.accessibilityTraits = accessibilityTraits
@ -42,9 +42,7 @@ class Label: UILabel, MVMCoreViewProtocol, MVMCoreUIMoleculeViewProtocol {
} }
var hasText: Bool { var hasText: Bool {
guard let text = text, let attributedText = attributedText else { return false } guard let text = text, let attributedText = attributedText else { return false }
return !text.isEmpty || !attributedText.string.isEmpty return !text.isEmpty || !attributedText.string.isEmpty
} }
@ -53,7 +51,6 @@ class Label: UILabel, MVMCoreViewProtocol, MVMCoreUIMoleculeViewProtocol {
//------------------------------------------------------ //------------------------------------------------------
func setupView() { func setupView() {
backgroundColor = .clear backgroundColor = .clear
numberOfLines = 0 numberOfLines = 0
lineBreakMode = .byWordWrapping lineBreakMode = .byWordWrapping
@ -84,55 +81,55 @@ class Label: UILabel, MVMCoreViewProtocol, MVMCoreUIMoleculeViewProtocol {
// MARK: - Functions // MARK: - Functions
//------------------------------------------------------ //------------------------------------------------------
class func commonLabelH1(_ scale: Bool) -> Label { static func commonLabelH1(_ scale: Bool) -> Label {
let label = Label.label() let label = Label.label()
label.styleH1(scale) label.styleH1(scale)
return label return label
} }
class func commonLabelH2(_ scale: Bool) -> Label { static func commonLabelH2(_ scale: Bool) -> Label {
let label = Label.label() let label = Label.label()
label.styleH2(scale) label.styleH2(scale)
return label return label
} }
class func commonLabelH3(_ scale: Bool) -> Label { static func commonLabelH3(_ scale: Bool) -> Label {
let label = Label.label() let label = Label.label()
label.styleH3(scale) label.styleH3(scale)
return label return label
} }
class func commonLabelH32(_ scale: Bool) -> Label { static func commonLabelH32(_ scale: Bool) -> Label {
let label = Label.label() let label = Label.label()
label.styleH32(scale) label.styleH32(scale)
return label return label
} }
class func commonLabelB1(_ scale: Bool) -> Label { static func commonLabelB1(_ scale: Bool) -> Label {
let label = Label.label() let label = Label.label()
label.styleB1(scale) label.styleB1(scale)
return label return label
} }
class func commonLabelB2(_ scale: Bool) -> Label { static func commonLabelB2(_ scale: Bool) -> Label {
let label = Label.label() let label = Label.label()
label.styleB2(scale) label.styleB2(scale)
return label return label
} }
class func commonLabelB3(_ scale: Bool) -> Label { static func commonLabelB3(_ scale: Bool) -> Label {
let label = Label.label() let label = Label.label()
label.styleB3(scale) label.styleB3(scale)
return label return label
} }
class func commonLabelB20(_ scale: Bool) -> Label { static func commonLabelB20(_ scale: Bool) -> Label {
let label = Label.label() let label = Label.label()
label.styleB20(scale) label.styleB20(scale)
return label return label
} }
class func label() -> Label { static func label() -> Label {
return Label(frame: CGRect.zero) return Label(frame: CGRect.zero)
} }
@ -141,21 +138,16 @@ class Label: UILabel, MVMCoreViewProtocol, MVMCoreUIMoleculeViewProtocol {
//------------------------------------------------------ //------------------------------------------------------
static func setLabel(_ label: UILabel?, withHTML html: String?) { static func setLabel(_ label: UILabel?, withHTML html: String?) {
let data: Data? = html?.data(using: .utf8) let data: Data? = html?.data(using: .utf8)
if data != nil {
let error: Error? = nil if let data = data {
if let data = data { do {
label?.attributedText = try? NSAttributedString(data: data, label?.attributedText = try NSAttributedString(data: data,
options: [NSAttributedString.DocumentReadingOptionKey.documentType:NSAttributedString.DocumentType.html, NSAttributedString.DocumentReadingOptionKey.characterEncoding: String.Encoding.utf8.rawValue], options: [NSAttributedString.DocumentReadingOptionKey.documentType:NSAttributedString.DocumentType.html, NSAttributedString.DocumentReadingOptionKey.characterEncoding: String.Encoding.utf8.rawValue],
documentAttributes: nil) documentAttributes: nil)
} catch {
// compare with above... MVMCoreUILoggingHandler.shared()?.addError(toLog: MVMCoreErrorObject.createErrorObject(for: error, location: "LabelHTMLParse")!)
// 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")!)
} }
} }
} }
@ -178,24 +170,24 @@ class Label: UILabel, MVMCoreViewProtocol, MVMCoreUIMoleculeViewProtocol {
label.accessibilityLabel = accessibilityText label.accessibilityLabel = accessibilityText
} }
let fontSize = json?.floatForKey("fontSize") let fontSize = json?.floatForKey("fontSize") ?? 0.0
if let fontName = json?.optionalStringForKey("fontName"), let fontSize = fontSize { if let fontName = json?.optionalStringForKey("fontName") {
label.font = MFFonts.mfFont(withName: fontName, size: CGFloat(fontSize) == 0.0 ? label.font.pointSize : CGFloat(fontSize)) label.font = MFFonts.mfFont(withName: fontName, size: fontSize == 0.0 ? label.font.pointSize : CGFloat(fontSize))
} else if let fontSize = fontSize { } else {
label.font = label.font.withSize(CGFloat(fontSize)) label.font = label.font.withSize(CGFloat(fontSize))
} }
if let attributes = json?.arrayForKey("attributes"), let labelText = label.text { if let attributes = json?.arrayForKey("attributes"), let labelText = label.text {
let attributedString = NSMutableAttributedString(string: labelText, attributes: [NSAttributedString.Key.font: label.font, let attributedString = NSMutableAttributedString(string: labelText, attributes: [NSAttributedString.Key.font: label.font as Any,
NSAttributedString.Key.foregroundColor: label.textColor]) NSAttributedString.Key.foregroundColor: label.textColor as Any])
for case let attribute as [String: Any] in attributes { for case let attribute as [String: Any] in attributes {
let location = attribute.floatForKey("location") let range = NSRange(location: Int(attribute.floatForKey("location")),
let length = attribute.floatForKey("length") length: Int(attribute.floatForKey("length")))
let range = NSRange(location: Int(location), length: Int(length)) guard let attributeType = attribute.optionalStringForKey(KeyType) else { continue }
switch attribute.stringForkey(KeyType){ switch attributeType {
case "underline": case "underline":
attributedString.addAttribute(.underlineStyle, value: NSUnderlineStyle.single, range: range) attributedString.addAttribute(.underlineStyle, value: NSUnderlineStyle.single, range: range)
@ -208,7 +200,7 @@ class Label: UILabel, MVMCoreViewProtocol, MVMCoreUIMoleculeViewProtocol {
} }
case "font": case "font":
let fontSize = attribute.floatForKey("size") let fontSize = attribute.floatForKey("size")
var font: UIFont? let font: UIFont?
if let fontName = attribute.optionalStringForKey("name"), !fontName.isEmpty { if let fontName = attribute.optionalStringForKey("name"), !fontName.isEmpty {
font = MFFonts.mfFont(withName: fontName, size: fontSize != 0.0 ? CGFloat(fontSize) : CGFloat(label.font.pointSize)) font = MFFonts.mfFont(withName: fontName, size: fontSize != 0.0 ? CGFloat(fontSize) : CGFloat(label.font.pointSize))
@ -216,9 +208,8 @@ class Label: UILabel, MVMCoreViewProtocol, MVMCoreUIMoleculeViewProtocol {
font = label.font.withSize(CGFloat(fontSize)) font = label.font.withSize(CGFloat(fontSize))
} }
if font != nil { attributedString.addAttribute(.font, value: font as Any, range: range)
attributedString.addAttribute(.font, value: font as Any, range: range)
}
default: default:
continue continue
} }
@ -273,38 +264,30 @@ class Label: UILabel, MVMCoreViewProtocol, MVMCoreUIMoleculeViewProtocol {
} }
func updateView(_ size: CGFloat) { func updateView(_ size: CGFloat) {
scaleSize = size scaleSize = size
if let originalAttributedString = originalAttributedString { if let originalAttributedString = originalAttributedString {
let attributedString = NSMutableAttributedString(attributedString: originalAttributedString) let attributedString = NSMutableAttributedString(attributedString: originalAttributedString)
attributedString.removeAttribute(.font, range: NSRange(location: 0, length: attributedString.length)) 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 originalAttributedString.enumerateAttribute(.font, in: NSRange(location: 0, length: originalAttributedString.length), options: [], using: { value, range, stop in
// Loop the original attributed string, resize the fonts. // Loop the original attributed string, resize the fonts.
if let fontObj = value as? UIFont, let stylerSize = MFStyler.sizeObjectGeneric(forCurrentDevice: fontObj.pointSize)?.getValueBased(onSize: size) { 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) attributedString.addAttribute(.font, value: fontObj.withSize(stylerSize) as Any, range: range)
} }
}) })
attributedText = attributedString attributedText = attributedString
} else if !MVMCoreGetterUtility.fequal(a: Float(standardFontSize), b: 0.0) { } else if !MVMCoreGetterUtility.fequal(a: Float(standardFontSize), b: 0.0), let sizeObject: MFSizeObject = self.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))
}
} }
} }
func setFont(_ font: UIFont, scale: Bool) { func setFont(_ font: UIFont, scale: Bool) {
self.font = font self.font = font
setScale(scale) setScale(scale)
} }
func setScale(_ scale: Bool) { func setScale(_ scale: Bool) {
if scale { if scale {
standardFontSize = font.pointSize standardFontSize = font.pointSize
updateView(scaleSize ?? MVMCoreUISplitViewController.getApplicationViewWidth()) updateView(scaleSize ?? MVMCoreUISplitViewController.getApplicationViewWidth())
@ -318,11 +301,17 @@ class Label: UILabel, MVMCoreViewProtocol, MVMCoreUIMoleculeViewProtocol {
//------------------------------------------------------ //------------------------------------------------------
func setAsMolecule() { func setAsMolecule() {
// Empty
setupView()
} }
func setWithJSON(_ json: [AnyHashable: Any]?, delegate: NSObject?, additionalData: [AnyHashable: Any]?) { func setWithJSON(_ json: [AnyHashable: Any]?, delegate: NSObject?, additionalData: [AnyHashable: Any]?) {
Label.setUILabel(self, withJSON: json, delegate: delegate, additionalData: additionalData) Label.setUILabel(self, withJSON: json, delegate: delegate, additionalData: additionalData)
originalAttributedString = attributedText originalAttributedString = attributedText
if let actionText = dictionary["actionText"] as? String {
self.actionText = actionText
}
} }
} }