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?) {
let data: Data? = html?.data(using: .utf8)
guard let data = html?.data(using: .utf8) else { return }
if let data = data {
do {
label?.attributedText = try NSAttributedString(data: data,
options: [NSAttributedString.DocumentReadingOptionKey.documentType:NSAttributedString.DocumentType.html, NSAttributedString.DocumentReadingOptionKey.characterEncoding: String.Encoding.utf8.rawValue],
documentAttributes: nil)
} catch {
MVMCoreUILoggingHandler.shared()?.addError(toLog: MVMCoreErrorObject.createErrorObject(for: error, location: "LabelHTMLParse")!)
do {
label?.attributedText = try NSAttributedString(data: data,
options: [NSAttributedString.DocumentReadingOptionKey.documentType:NSAttributedString.DocumentType.html, NSAttributedString.DocumentReadingOptionKey.characterEncoding: String.Encoding.utf8.rawValue],
documentAttributes: nil)
} catch {
if let coreErrorObject = 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]?) {
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") ?? 0.0
if let fontName = json?.optionalStringForKey("fontName") {
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 {
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")))
guard let label = label else { return }
label.text = json?.optionalStringForKey(KeyText)
setLabel(label, withHTML: json?.stringForkey("html"))
if let textColorHex = json?.optionalStringForKey(KeyTextColor), !textColorHex.isEmpty {
label.textColor = UIColor.mfGet(forHex: textColorHex)
}
if let backgroundColorHex = json?.optionalStringForKey(KeyBackgroundColor), !backgroundColorHex.isEmpty {
label.backgroundColor = UIColor.mfGet(forHex: backgroundColorHex)
}
label.accessibilityLabel = json?.optionalStringForKey("accessibilityText")
let fontSize = json?.floatForKey("fontSize") ?? 0.0
if let fontName = json?.optionalStringForKey("fontName") {
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 {
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")))
guard let attributeType = attribute.optionalStringForKey(KeyType) else { continue }
switch attributeType {
case "underline":
attributedString.addAttribute(.underlineStyle, value: NSUnderlineStyle.single, range: range)
guard let attributeType = attribute.optionalStringForKey(KeyType) else { continue }
case "strikethrough":
attributedString.addAttribute(.strikethroughStyle, value: NSUnderlineStyle.thick, range: range)
switch attributeType {
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")
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
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")
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
if let actionText = dictionary["actionText"] as? String {
self.actionText = actionText
}
// if let actionText = dictionary["actionText"] as? String {
// self.actionText = actionText
// }
}
}