optional check improvements.

This commit is contained in:
Christiano, Kevin 2019-04-12 12:23:55 -04:00
parent d6fdf9958e
commit 71f1f499e0

View File

@ -139,83 +139,81 @@ 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) guard let data = html?.data(using: .utf8) else { return }
if let data = data { do {
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 {
} catch { if let coreErrorObject = MVMCoreErrorObject.createErrorObject(for: error, location: "LabelHTMLParse") {
MVMCoreUILoggingHandler.shared()?.addError(toLog: MVMCoreErrorObject.createErrorObject(for: error, location: "LabelHTMLParse")!) MVMCoreUILoggingHandler.shared()?.addError(toLog: coreErrorObject)
} }
} }
} }
static func setUILabel(_ label: UILabel?, withJSON json: [AnyHashable: Any]?, delegate: NSObject?, additionalData: [AnyHashable: Any]?) { static func setUILabel(_ label: UILabel?, withJSON json: [AnyHashable: Any]?, delegate: NSObject?, additionalData: [AnyHashable: Any]?) {
if let label = label { guard let label = label else { return }
label.text = json?.optionalStringForKey(KeyText)
setLabel(label, withHTML: json?.stringForkey("html"))
if let textColorHex = json?.optionalStringForKey(KeyTextColor) { label.text = json?.optionalStringForKey(KeyText)
label.textColor = UIColor.mfGet(forHex: textColorHex) setLabel(label, withHTML: json?.stringForkey("html"))
}
if let backgroundColorHex = json?.optionalStringForKey(KeyBackgroundColor) { if let textColorHex = json?.optionalStringForKey(KeyTextColor), !textColorHex.isEmpty {
label.backgroundColor = UIColor.mfGet(forHex: backgroundColorHex) label.textColor = UIColor.mfGet(forHex: textColorHex)
} }
if let accessibilityText = json?.optionalStringForKey("accessibilityText") { if let backgroundColorHex = json?.optionalStringForKey(KeyBackgroundColor), !backgroundColorHex.isEmpty {
label.accessibilityLabel = accessibilityText label.backgroundColor = UIColor.mfGet(forHex: backgroundColorHex)
} }
let fontSize = json?.floatForKey("fontSize") ?? 0.0 label.accessibilityLabel = json?.optionalStringForKey("accessibilityText")
if let fontName = json?.optionalStringForKey("fontName") { let fontSize = json?.floatForKey("fontSize") ?? 0.0
label.font = MFFonts.mfFont(withName: fontName, size: fontSize == 0.0 ? label.font.pointSize : CGFloat(fontSize))
} else {
label.font = label.font.withSize(CGFloat(fontSize))
}
if let attributes = json?.arrayForKey("attributes"), let labelText = label.text { if let fontName = json?.optionalStringForKey("fontName") {
let attributedString = NSMutableAttributedString(string: labelText, attributes: [NSAttributedString.Key.font: label.font as Any, label.font = MFFonts.mfFont(withName: fontName, size: fontSize == 0.0 ? label.font.pointSize : CGFloat(fontSize))
NSAttributedString.Key.foregroundColor: label.textColor as Any]) } else {
for case let attribute as [String: Any] in attributes { label.font = label.font.withSize(CGFloat(fontSize))
let range = NSRange(location: Int(attribute.floatForKey("location")), }
length: Int(attribute.floatForKey("length")))
guard let attributeType = attribute.optionalStringForKey(KeyType) else { continue } if let attributes = json?.arrayForKey("attributes"), let labelText = label.text {
let attributedString = NSMutableAttributedString(string: labelText, attributes: [NSAttributedString.Key.font: label.font as Any,
NSAttributedString.Key.foregroundColor: label.textColor as Any])
for case let attribute as [String: Any] in attributes {
let range = NSRange(location: Int(attribute.floatForKey("location")),
length: Int(attribute.floatForKey("length")))
switch attributeType { guard let attributeType = attribute.optionalStringForKey(KeyType) else { continue }
case "underline":
attributedString.addAttribute(.underlineStyle, value: NSUnderlineStyle.single, range: range)
case "strikethrough": switch attributeType {
attributedString.addAttribute(.strikethroughStyle, value: NSUnderlineStyle.thick, range: range) case "underline":
attributedString.addAttribute(.underlineStyle, value: NSUnderlineStyle.single, range: range)
case "color": case "strikethrough":
if let colorHex = attribute.optionalStringForKey(KeyTextColor), !colorHex.isEmpty { attributedString.addAttribute(.strikethroughStyle, value: NSUnderlineStyle.thick, range: range)
attributedString.addAttribute(.foregroundColor, value: UIColor.mfGet(forHex: colorHex), range: range)
}
case "font":
let fontSize = attribute.floatForKey("size")
let font: UIFont?
if let fontName = attribute.optionalStringForKey("name"), !fontName.isEmpty { case "color":
font = MFFonts.mfFont(withName: fontName, size: fontSize != 0.0 ? CGFloat(fontSize) : CGFloat(label.font.pointSize)) if let colorHex = attribute.optionalStringForKey(KeyTextColor), !colorHex.isEmpty {
} else { attributedString.addAttribute(.foregroundColor, value: UIColor.mfGet(forHex: colorHex), range: range)
font = label.font.withSize(CGFloat(fontSize))
}
attributedString.addAttribute(.font, value: font as Any, range: range)
default:
continue
} }
case "font":
let fontSize = attribute.floatForKey("size")
let 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))
}
attributedString.addAttribute(.font, value: font as Any, range: range)
default:
continue
} }
label.attributedText = attributedString
} }
label.attributedText = attributedString
} }
} }
@ -310,8 +308,8 @@ class Label: UILabel, MVMCoreViewProtocol, MVMCoreUIMoleculeViewProtocol {
originalAttributedString = attributedText originalAttributedString = attributedText
if let actionText = dictionary["actionText"] as? String { // if let actionText = dictionary["actionText"] as? String {
self.actionText = actionText // self.actionText = actionText
} // }
} }
} }