stopping for now.
This commit is contained in:
parent
08dc333568
commit
ce68eafd34
@ -59,6 +59,7 @@ class Label: UILabel, MVMCoreViewProtocol, MVMCoreUIMoleculeViewProtocol {
|
|||||||
//------------------------------------------------------
|
//------------------------------------------------------
|
||||||
|
|
||||||
func setupView() {
|
func setupView() {
|
||||||
|
|
||||||
backgroundColor = UIColor.clear
|
backgroundColor = UIColor.clear
|
||||||
numberOfLines = 0
|
numberOfLines = 0
|
||||||
lineBreakMode = NSLineBreakMode.byWordWrapping
|
lineBreakMode = NSLineBreakMode.byWordWrapping
|
||||||
@ -85,24 +86,32 @@ class Label: UILabel, MVMCoreViewProtocol, MVMCoreUIMoleculeViewProtocol {
|
|||||||
standardFontSize = size
|
standardFontSize = size
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//------------------------------------------------------
|
||||||
|
// MARK: - Functions
|
||||||
|
//------------------------------------------------------
|
||||||
|
|
||||||
func updateView(_ size: CGFloat) {
|
func updateView(_ size: CGFloat) {
|
||||||
|
|
||||||
scaleSize = NSNumber(value: Float(size))
|
scaleSize = NSNumber(value: Float(size))
|
||||||
|
|
||||||
if let originalAttributedString = originalAttributedString {
|
if let originalAttributedString = originalAttributedString {
|
||||||
var 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.
|
||||||
let font: UIFont? = (value? as AnyObject).withSize(MFStyler.sizeObjectGeneric(forCurrentDevice: value?.pointSize).getValueBased(onSize: size))
|
let font: UIFont? = (value as? UIFont)?.withSize((MFStyler.sizeObjectGeneric(forCurrentDevice: ((value as? UIFont)?.pointSize)!)?.getValueBased(onSize: size))!)
|
||||||
attributedString.addAttribute(.font, value: font, range: range)
|
attributedString.addAttribute(.font, value: font as Any, range: range)
|
||||||
})
|
})
|
||||||
attributedText = attributedString
|
attributedText = attributedString
|
||||||
} else if !fequal(standardFontSize, 0) {
|
|
||||||
|
} else if !MVMCoreGetterUtility.fequal(a: Float(standardFontSize), b: 0.0) {
|
||||||
var sizeObject: MFSizeObject? = self.sizeObject
|
var sizeObject: MFSizeObject? = self.sizeObject
|
||||||
|
|
||||||
if sizeObject == nil {
|
if sizeObject == nil {
|
||||||
sizeObject = MFStyler.sizeObjectGeneric(forCurrentDevice: standardFontSize)
|
sizeObject = MFStyler.sizeObjectGeneric(forCurrentDevice: standardFontSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
self.font = self.font.withSize(sizeObject?.getValueBased(onSize: size) ?? 0.0)
|
self.font = self.font.withSize(sizeObject?.getValueBased(onSize: size) ?? 0.0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -115,7 +124,11 @@ class Label: UILabel, MVMCoreViewProtocol, MVMCoreUIMoleculeViewProtocol {
|
|||||||
func setScale(_ scale: Bool) {
|
func setScale(_ scale: Bool) {
|
||||||
if scale {
|
if scale {
|
||||||
standardFontSize = font.pointSize
|
standardFontSize = font.pointSize
|
||||||
updateView((scaleSize ? CGFloat(scaleSize?.floatValue ?? 0) : MVMCoreUISplitViewController.getApplicationViewWidth()))
|
if let scaleSize = scaleSize {
|
||||||
|
updateView(CGFloat(scaleSize.floatValue))
|
||||||
|
} else {
|
||||||
|
updateView(MVMCoreUISplitViewController.getApplicationViewWidth())
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
standardFontSize = 0
|
standardFontSize = 0
|
||||||
}
|
}
|
||||||
@ -148,7 +161,7 @@ class Label: UILabel, MVMCoreViewProtocol, MVMCoreUIMoleculeViewProtocol {
|
|||||||
case commonLabelB1(scale: Bool)
|
case commonLabelB1(scale: Bool)
|
||||||
case commonLabelTopAlert(scale: Bool)
|
case commonLabelTopAlert(scale: Bool)
|
||||||
|
|
||||||
func getLabel() -> Label {
|
func createLabel() -> Label {
|
||||||
|
|
||||||
let label = Label()
|
let label = Label()
|
||||||
|
|
||||||
@ -221,6 +234,10 @@ class Label: UILabel, MVMCoreViewProtocol, MVMCoreUIMoleculeViewProtocol {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//------------------------------------------------------
|
||||||
|
// MARK: - Methods
|
||||||
|
//------------------------------------------------------
|
||||||
|
|
||||||
func styleB2(_ scale: Bool) {
|
func styleB2(_ scale: Bool) {
|
||||||
MFStyler.styleLabelB2(self, genericScaling: false)
|
MFStyler.styleLabelB2(self, genericScaling: false)
|
||||||
setScale(scale)
|
setScale(scale)
|
||||||
@ -344,14 +361,14 @@ class Label: UILabel, MVMCoreViewProtocol, MVMCoreUIMoleculeViewProtocol {
|
|||||||
class func setLabel(_ label: UILabel?, withHTML html: String?) {
|
class func setLabel(_ label: UILabel?, withHTML html: String?) {
|
||||||
let data: Data? = html?.data(using: .utf8)
|
let data: Data? = html?.data(using: .utf8)
|
||||||
if data != nil {
|
if data != nil {
|
||||||
var error: Error? = nil
|
let error: Error? = nil
|
||||||
if let data = data {
|
if let data = data {
|
||||||
label?.attributedText = try? NSAttributedString(data: data,
|
label?.attributedText = try? NSAttributedString(data: data,
|
||||||
options: [NSAttributedString.DocumentAttributeKey.documentType: NSAttributedString.DocumentType.html.rawValue],
|
options: [NSAttributedString.DocumentReadingOptionKey.documentType:NSAttributedString.DocumentType.html, NSAttributedString.DocumentReadingOptionKey.characterEncoding: String.Encoding.utf8.rawValue],
|
||||||
documentAttributes: nil)
|
documentAttributes: nil)
|
||||||
}
|
}
|
||||||
if let labelError = error {
|
if let labelError = error {
|
||||||
try? MVMCoreUILoggingHandler.shared()?.addError(toLog: MVMCoreErrorObject.createErrorObject(for: labelError, location: "LabelHTMLParse"))
|
try? MVMCoreUILoggingHandler.shared()?.addError(toLog: MVMCoreErrorObject.createErrorObject(for: labelError, location: "LabelHTMLParse")!)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -374,7 +391,7 @@ class Label: UILabel, MVMCoreViewProtocol, MVMCoreUIMoleculeViewProtocol {
|
|||||||
label.accessibilityLabel = accessibilityText
|
label.accessibilityLabel = accessibilityText
|
||||||
}
|
}
|
||||||
|
|
||||||
var fontSize = json?.floatForKey("fontSize")
|
let fontSize = json?.floatForKey("fontSize")
|
||||||
|
|
||||||
if let fontName = json?.optionalStringForKey("fontName"), let fontSize = 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))
|
label.font = MFFonts.mfFont(withName: fontName, size: CGFloat(fontSize) == 0.0 ? label.font.pointSize : CGFloat(fontSize))
|
||||||
@ -382,42 +399,41 @@ class Label: UILabel, MVMCoreViewProtocol, MVMCoreUIMoleculeViewProtocol {
|
|||||||
label.font = label.font.withSize(CGFloat(fontSize))
|
label.font = label.font.withSize(CGFloat(fontSize))
|
||||||
}
|
}
|
||||||
|
|
||||||
if let attributes = json?.arrayForKey("attributes") {
|
if let attributes = json?.arrayForKey("attributes"), let labelText = label.text {
|
||||||
var attributedString = NSMutableAttributedString(string: label.text, attributes: [NSAttributedString.Key.font: label.font,
|
let attributedString = NSMutableAttributedString(string: labelText, attributes: [NSAttributedString.Key.font: label.font,
|
||||||
NSAttributedString.Key.foregroundColor: label.textColor])
|
NSAttributedString.Key.foregroundColor: label.textColor])
|
||||||
for case let attribute as [String: Any] in attributes {
|
for case let attribute as [String: Any] in attributes {
|
||||||
var location = attribute.optionalNumber(forKey: "location")
|
let location = attribute.floatForKey("location")
|
||||||
var length = attribute.optionalNumber(forKey: "length")
|
let length = attribute.floatForKey("length")
|
||||||
if location && length {
|
|
||||||
|
let range = NSRange(location: Int(location), length: Int(length))
|
||||||
|
|
||||||
|
switch attribute.stringForkey(KeyType){
|
||||||
|
case "underline":
|
||||||
|
attributedString.addAttribute(.underlineStyle, value: NSUnderlineStyle.single, range: range)
|
||||||
|
|
||||||
var range = NSRange(location: Int(location.uintValue), length: Int(length.uintValue))
|
case "strikethrough":
|
||||||
var type = attribute.string(KeyType)
|
attributedString.addAttribute(.strikethroughStyle, value: NSUnderlineStyle.thick, range: range)
|
||||||
|
|
||||||
if type == "underline" {
|
case "color":
|
||||||
attributedString.addAttribute(.underlineStyle, value: NSUnderlineStyle.single, range: range)
|
if let colorHex = attribute.optionalStringForKey(KeyTextColor), !colorHex.isEmpty {
|
||||||
|
attributedString.addAttribute(.foregroundColor, value: UIColor.mfGet(forHex: colorHex), 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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
case "font":
|
||||||
|
let 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 as Any, range: range)
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
label.attributedText = attributedString
|
label.attributedText = attributedString
|
||||||
@ -425,6 +441,14 @@ class Label: UILabel, MVMCoreViewProtocol, MVMCoreUIMoleculeViewProtocol {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//------------------------------------------------------
|
||||||
|
// MARK: - Atomization
|
||||||
|
//------------------------------------------------------
|
||||||
|
|
||||||
|
func setAsMolecule() {
|
||||||
|
// Empty
|
||||||
|
}
|
||||||
|
|
||||||
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
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user