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