Further work on conversion.
This commit is contained in:
parent
656652b962
commit
08dc333568
@ -42,7 +42,16 @@ class Label: UILabel, MVMCoreViewProtocol, MVMCoreUIMoleculeViewProtocol {
|
||||
}
|
||||
|
||||
var hasText: Bool {
|
||||
return text.length > 0 || (attributedText?.length ?? 0) > 0
|
||||
|
||||
if let text = text, !text.isEmpty {
|
||||
return true
|
||||
}
|
||||
|
||||
if let attributedText = attributedText, !attributedText.string.isEmpty {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
//------------------------------------------------------
|
||||
@ -57,6 +66,7 @@ class Label: UILabel, MVMCoreViewProtocol, MVMCoreUIMoleculeViewProtocol {
|
||||
}
|
||||
|
||||
init() {
|
||||
super.init(frame: .zero)
|
||||
setupView()
|
||||
}
|
||||
|
||||
@ -79,12 +89,12 @@ class Label: UILabel, MVMCoreViewProtocol, MVMCoreUIMoleculeViewProtocol {
|
||||
|
||||
scaleSize = NSNumber(value: Float(size))
|
||||
|
||||
if originalAttributedString {
|
||||
if let originalAttributedString = originalAttributedString {
|
||||
var 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?.withSize(MFStyler.sizeObjectGeneric(forCurrentDevice: value?.pointSize).getValueBased(onSize: size))
|
||||
let font: UIFont? = (value? as AnyObject).withSize(MFStyler.sizeObjectGeneric(forCurrentDevice: value?.pointSize).getValueBased(onSize: size))
|
||||
attributedString.addAttribute(.font, value: font, range: range)
|
||||
})
|
||||
attributedText = attributedString
|
||||
@ -115,155 +125,100 @@ class Label: UILabel, MVMCoreViewProtocol, MVMCoreUIMoleculeViewProtocol {
|
||||
return Label(frame: CGRect.zero)
|
||||
}
|
||||
|
||||
class func commonLabelB2(_ scale: Bool) -> Label {
|
||||
let label = Label.label()
|
||||
label.styleB2(scale)
|
||||
return label
|
||||
}
|
||||
|
||||
class func commonLabelBody(withSize size: CGFloat, scale: Bool) -> Label {
|
||||
let label = Label.label()
|
||||
label.styleBody(withSize: size, scale: scale)
|
||||
return label
|
||||
}
|
||||
|
||||
class func commonLabelBodyBold(_ scale: Bool) -> Label {
|
||||
let label = Label()
|
||||
label.styleBodyBold(scale)
|
||||
return label
|
||||
}
|
||||
|
||||
class func commonLabelBodyLighter(_ scale: Bool) -> Label {
|
||||
let label = Label()
|
||||
label.styleBodyLighter(scale)
|
||||
return label
|
||||
}
|
||||
|
||||
class func commonLabelBodyLarge(_ scale: Bool) -> Label {
|
||||
let label = Label()
|
||||
label.styleBodyLarge(scale)
|
||||
return label
|
||||
}
|
||||
|
||||
class func commonLabelH1(_ scale: Bool) -> Label {
|
||||
let label = Label()
|
||||
label.styleH1(scale)
|
||||
return label
|
||||
}
|
||||
|
||||
class func commonLabelH2(_ scale: Bool) -> Label {
|
||||
let label = Label()
|
||||
label.styleH2(scale)
|
||||
return label
|
||||
}
|
||||
|
||||
class func commonLabelHeadlineSmall(_ scale: Bool) -> Label {
|
||||
let label = Label()
|
||||
label.styleHeadlineSmall(scale)
|
||||
return label
|
||||
}
|
||||
|
||||
class func commonLabelHeadline(withSize size: CGFloat, scale: Bool) -> Label {
|
||||
let label = Label()
|
||||
label.styleHeadline(withSize: size, scale: scale)
|
||||
return label
|
||||
}
|
||||
|
||||
class func commonLabelHeadlineBlack(withSize size: CGFloat, scale: Bool) -> Label {
|
||||
let label = Label()
|
||||
label.styleHeadlineBlack(withSize: size, scale: scale)
|
||||
return label
|
||||
}
|
||||
|
||||
class func commonLabelH3(_ scale: Bool) -> Label {
|
||||
let label = Label()
|
||||
label.styleH3(scale)
|
||||
return label
|
||||
}
|
||||
|
||||
class func commonLabelSubheadBold(_ scale: Bool) -> Label {
|
||||
let label = Label()
|
||||
label.styleSubheadBold(scale)
|
||||
return label
|
||||
}
|
||||
|
||||
class func commonLabelSubheadBoldLarge(_ scale: Bool) -> Label {
|
||||
let label = Label()
|
||||
label.styleSubheadBoldLarge(scale)
|
||||
return label
|
||||
}
|
||||
|
||||
class func commonLabelB3(_ scale: Bool) -> Label {
|
||||
let label = Label()
|
||||
label.styleB3(scale)
|
||||
return label
|
||||
}
|
||||
|
||||
class func commonLabelFeedMessage(_ scale: Bool) -> Label {
|
||||
let label = Label()
|
||||
label.styleFeedMessage(scale)
|
||||
return label
|
||||
}
|
||||
|
||||
class func commonLabelFeedSubMessage(_ scale: Bool) -> Label {
|
||||
let label = Label()
|
||||
label.styleFeedSubMessage(scale)
|
||||
return label
|
||||
}
|
||||
|
||||
class func commonLabelFeedHeadline(_ scale: Bool) -> Label {
|
||||
let label = Label()
|
||||
label.styleFeedHeadline(scale)
|
||||
return label
|
||||
}
|
||||
|
||||
class func commonLabelFeedTitle(_ scale: Bool) -> Label {
|
||||
let label = Label()
|
||||
label.styleFeedTitle(scale)
|
||||
return label
|
||||
}
|
||||
|
||||
class func commonLabelPlanCardTitle(_ scale: Bool) -> Label {
|
||||
let label = Label()
|
||||
label.stylePlanCardTitle(scale)
|
||||
return label
|
||||
}
|
||||
|
||||
class func commonLabelB1(_ scale: Bool) -> Label {
|
||||
let label = Label()
|
||||
label.styleB1(scale)
|
||||
return label
|
||||
}
|
||||
|
||||
class func commonLabelTopAlert(_ scale: Bool) -> Label {
|
||||
let label = Label()
|
||||
label.styleTopAlert(scale)
|
||||
return label
|
||||
}
|
||||
|
||||
class func setLabel(_ label: UILabel?, withHTML html: String?) {
|
||||
let data: Data? = html?.data(using: .utf8)
|
||||
if data != nil {
|
||||
var error: Error? = nil
|
||||
if let data = data {
|
||||
label?.attributedText = try? NSAttributedString(data: data,
|
||||
options: [NSAttributedString.DocumentAttributeKey.documentType: NSAttributedString.DocumentType.html.rawValue as Any],
|
||||
documentAttributes: nil)
|
||||
}
|
||||
if let labelError = error {
|
||||
try? MVMCoreUILoggingHandler.shared()?.addError(toLog: MVMCoreErrorObject.createErrorObject(for: labelError, location: "LabelHTMLParse"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class func setUILabel(_ label: UILabel?, withJSON json: [AnyHashable: Any]?, delegate: NSObject?, additionalData: [AnyHashable: Any]?) {
|
||||
enum Style {
|
||||
case commonLabelB2(scale: Bool)
|
||||
case commonLabelBody(size: CGFloat, scale: Bool)
|
||||
case commonLabelBodyBold(scale: Bool)
|
||||
case commonLabelBodyLighter(scale: Bool)
|
||||
case commonLabelBodyLarge(scale: Bool)
|
||||
case commonLabelH1(scale: Bool)
|
||||
case commonLabelH2(scale: Bool)
|
||||
case commonLabelHeadlineSmall(scale: Bool)
|
||||
case commonLabelHeadline(size: CGFloat, scale: Bool)
|
||||
case commonLabelHeadlineBlack(size: CGFloat, scale: Bool)
|
||||
case commonLabelH3(scale: Bool)
|
||||
case commonLabelSubheadBold(scale: Bool)
|
||||
case commonLabelSubheadBoldLarge(scale: Bool)
|
||||
case commonLabelB3(scale: Bool)
|
||||
case commonLabelFeedMessage(scale: Bool)
|
||||
case commonLabelFeedSubMessage(scale: Bool)
|
||||
case commonLabelFeedHeadline(scale: Bool)
|
||||
case commonLabelFeedTitle(scale: Bool)
|
||||
case commonLabelPlanCardTitle(scale: Bool)
|
||||
case commonLabelB1(scale: Bool)
|
||||
case commonLabelTopAlert(scale: Bool)
|
||||
|
||||
// TODO: !!!
|
||||
}
|
||||
|
||||
func setWithJSON(_ json: [AnyHashable: Any]?, delegate: NSObject?, additionalData: [AnyHashable: Any]?) {
|
||||
Label.setUILabel(self, withJSON: json, delegate: delegate, additionalData: additionalData)
|
||||
originalAttributedString = attributedText
|
||||
func getLabel() -> Label {
|
||||
|
||||
let label = Label()
|
||||
|
||||
switch self {
|
||||
case .commonLabelB2(let scale):
|
||||
label.styleB2(scale)
|
||||
|
||||
case .commonLabelBody(let size, let scale):
|
||||
label.styleBody(withSize: size, scale: scale)
|
||||
|
||||
case .commonLabelBodyBold(let scale):
|
||||
label.styleBodyBold(scale)
|
||||
|
||||
case .commonLabelBodyLighter(let scale):
|
||||
label.styleBodyLighter(scale)
|
||||
|
||||
case .commonLabelBodyLarge(let scale):
|
||||
label.styleBodyLarge(scale)
|
||||
|
||||
case .commonLabelH1(let scale):
|
||||
label.styleH1(scale)
|
||||
|
||||
case .commonLabelH2(let scale):
|
||||
label.styleH2(scale)
|
||||
|
||||
case .commonLabelHeadlineSmall(let scale):
|
||||
label.styleHeadlineSmall(scale)
|
||||
|
||||
case .commonLabelHeadline(let size, let scale):
|
||||
label.styleHeadline(withSize: size, scale: scale)
|
||||
|
||||
case .commonLabelHeadlineBlack(let size, let scale):
|
||||
label.styleHeadlineBlack(withSize: size, scale: scale)
|
||||
|
||||
case .commonLabelH3(let scale):
|
||||
label.styleH3(scale)
|
||||
|
||||
case .commonLabelSubheadBold(let scale):
|
||||
label.styleSubheadBold(scale)
|
||||
|
||||
case .commonLabelSubheadBoldLarge(let scale):
|
||||
label.styleSubheadBoldLarge(scale)
|
||||
|
||||
case .commonLabelB3(let scale):
|
||||
label.styleB3(scale)
|
||||
|
||||
case .commonLabelFeedMessage(let scale):
|
||||
label.styleFeedMessage(scale)
|
||||
|
||||
case .commonLabelFeedSubMessage(let scale):
|
||||
label.styleFeedSubMessage(scale)
|
||||
|
||||
case .commonLabelFeedHeadline(let scale):
|
||||
label.styleFeedHeadline(scale)
|
||||
|
||||
case .commonLabelFeedTitle(let scale):
|
||||
label.styleFeedTitle(scale)
|
||||
|
||||
case .commonLabelPlanCardTitle(let scale):
|
||||
label.stylePlanCardTitle(scale)
|
||||
|
||||
case .commonLabelB1(let scale):
|
||||
label.styleB1(scale)
|
||||
|
||||
case .commonLabelTopAlert(let scale):
|
||||
label.styleTopAlert(scale)
|
||||
}
|
||||
|
||||
return label
|
||||
}
|
||||
}
|
||||
|
||||
func styleB2(_ scale: Bool) {
|
||||
@ -345,7 +300,7 @@ class Label: UILabel, MVMCoreViewProtocol, MVMCoreUIMoleculeViewProtocol {
|
||||
MFStyler.styleLabelFeedMessage(self, genericScaling: false)
|
||||
setScale(scale)
|
||||
}
|
||||
|
||||
|
||||
func styleFeedSubMessage(_ scale: Bool) {
|
||||
MFStyler.styleLabelFeedSubMessage(self, genericScaling: false)
|
||||
setScale(scale)
|
||||
@ -385,4 +340,93 @@ class Label: UILabel, MVMCoreViewProtocol, MVMCoreUIMoleculeViewProtocol {
|
||||
font = MFStyler.fontH1(false)
|
||||
setScale(scale)
|
||||
}
|
||||
|
||||
class func setLabel(_ label: UILabel?, withHTML html: String?) {
|
||||
let data: Data? = html?.data(using: .utf8)
|
||||
if data != nil {
|
||||
var error: Error? = nil
|
||||
if let data = data {
|
||||
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"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class 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
|
||||
}
|
||||
|
||||
var fontSize = json?.floatForKey("fontSize")
|
||||
|
||||
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 {
|
||||
label.font = label.font.withSize(CGFloat(fontSize))
|
||||
}
|
||||
|
||||
if let attributes = json?.arrayForKey("attributes") {
|
||||
var attributedString = NSMutableAttributedString(string: label.text, attributes: [NSAttributedString.Key.font: label.font,
|
||||
NSAttributedString.Key.foregroundColor: label.textColor])
|
||||
for case let attribute as [String: Any] in attributes {
|
||||
var location = attribute.optionalNumber(forKey: "location")
|
||||
var length = attribute.optionalNumber(forKey: "length")
|
||||
if location && length {
|
||||
|
||||
var range = NSRange(location: Int(location.uintValue), length: Int(length.uintValue))
|
||||
var type = attribute.string(KeyType)
|
||||
|
||||
if type == "underline" {
|
||||
attributedString.addAttribute(.underlineStyle, value: NSUnderlineStyle.single, range: range)
|
||||
|
||||
} else if type == "strikethrough" {
|
||||
attributedString.addAttribute(.strikethroughStyle, value: NSUnderlineStyle.thick, range: range)
|
||||
|
||||
} else if type == "color" {
|
||||
if let colorHex = attribute.optionalStringForKey(KeyTextColor), !colorHex.isEmpty {
|
||||
attributedString.addAttribute(.foregroundColor, value: UIColor.mfGet(forHex: colorHex), range: range)
|
||||
}
|
||||
} else if type == "font" {
|
||||
|
||||
var fontSize = attribute.floatForKey("size")
|
||||
var 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))
|
||||
}
|
||||
|
||||
if font != nil {
|
||||
attributedString.addAttribute(.font, value: font, range: range)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
label.attributedText = attributedString
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func setWithJSON(_ json: [AnyHashable: Any]?, delegate: NSObject?, additionalData: [AnyHashable: Any]?) {
|
||||
Label.setUILabel(self, withJSON: json, delegate: delegate, additionalData: additionalData)
|
||||
originalAttributedString = attributedText
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user