Mild fix to caretButton. Fixed dictionary and attributed text issues with Label.

This commit is contained in:
Christiano, Kevin 2019-04-24 13:32:13 -04:00
parent 7d12e70d46
commit d767890017
3 changed files with 40 additions and 29 deletions

View File

@ -8,7 +8,7 @@
// //
open class CaretButton: MFCustomButton { open class CaretButton: MFCustomButton, MVMCoreUIMoleculeViewProtocol {
//------------------------------------------------------ //------------------------------------------------------
// MARK: - Constants // MARK: - Constants
//------------------------------------------------------ //------------------------------------------------------
@ -114,7 +114,7 @@ open class CaretButton: MFCustomButton {
setTitleColor(disabledColor, for: .disabled) setTitleColor(disabledColor, for: .disabled)
} }
open func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: DelegateObject?, additionalData: [AnyHashable: Any]?) { @objc public func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: DelegateObject?, additionalData: [AnyHashable: Any]?) {
setWithActionMap(json, delegateObject: delegateObject, additionalData: additionalData) setWithActionMap(json, delegateObject: delegateObject, additionalData: additionalData)
guard let dictionary = json else { return } guard let dictionary = json else { return }

View File

@ -36,6 +36,7 @@ import MVMCore
//------------------------------------------------------ //------------------------------------------------------
@objc public func setupView() { @objc public func setupView() {
backgroundColor = .clear backgroundColor = .clear
numberOfLines = 0 numberOfLines = 0
lineBreakMode = .byWordWrapping lineBreakMode = .byWordWrapping
@ -115,7 +116,7 @@ import MVMCore
} }
@objc open class func label() -> Label { @objc open class func label() -> Label {
return Label(frame: CGRect.zero) return Label(frame: .zero)
} }
//------------------------------------------------------ //------------------------------------------------------
@ -128,7 +129,8 @@ import MVMCore
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") { if let coreErrorObject = MVMCoreErrorObject.createErrorObject(for: error, location: "LabelHTMLParse") {
@ -142,7 +144,8 @@ import MVMCore
guard let label = label else { return } guard let label = label else { return }
label.text = json?.optionalStringForKey(KeyText) label.text = json?.optionalStringForKey(KeyText)
setLabel(label, withHTML: json?.stringForkey("html"))
setLabel(label, withHTML: json?.optionalStringForKey("html"))
if let textColorHex = json?.optionalStringForKey(KeyTextColor), !textColorHex.isEmpty { if let textColorHex = json?.optionalStringForKey(KeyTextColor), !textColorHex.isEmpty {
label.textColor = UIColor.mfGet(forHex: textColorHex) label.textColor = UIColor.mfGet(forHex: textColorHex)
@ -163,37 +166,42 @@ import MVMCore
} }
if let attributes = json?.arrayForKey("attributes"), let labelText = label.text { if let attributes = json?.arrayForKey("attributes"), let labelText = label.text {
let attributedString = NSMutableAttributedString(string: labelText, attributes: [NSAttributedString.Key.font: label.font as Any, let attributedString = NSMutableAttributedString(string: labelText, attributes: [NSAttributedString.Key.font: label.font as UIFont,
NSAttributedString.Key.foregroundColor: label.textColor as Any]) NSAttributedString.Key.foregroundColor: label.textColor as UIColor])
for case let attribute as [String: Any] in attributes { for case let attribute as [String: Any] in attributes {
let range = NSRange(location: json?["location"] as? Int ?? 0, length: json?["length"] as? Int ?? 0) guard let attributeType = attribute.optionalStringForKey(KeyType),
let location = attribute["location"] as? Int,
guard let attributeType = attribute.optionalStringForKey(KeyType) else { continue } let length = attribute["length"] as? Int
else { continue }
let range = NSRange(location: location, length: length)
switch attributeType { switch attributeType {
case "underline": case "underline":
attributedString.addAttribute(.underlineStyle, value: NSUnderlineStyle.single, range: range) attributedString.addAttribute(.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: range)
case "strikethrough": case "strikethrough":
attributedString.addAttribute(.strikethroughStyle, value: NSUnderlineStyle.thick, range: range) attributedString.addAttribute(.strikethroughStyle, value: NSUnderlineStyle.thick.rawValue, range: range)
case "color": case "color":
if let colorHex = attribute.optionalStringForKey(KeyTextColor), !colorHex.isEmpty { if let colorHex = attribute.optionalStringForKey(KeyTextColor), !colorHex.isEmpty {
attributedString.removeAttribute(.foregroundColor, range: range)
attributedString.addAttribute(.foregroundColor, value: UIColor.mfGet(forHex: colorHex), range: range) attributedString.addAttribute(.foregroundColor, value: UIColor.mfGet(forHex: colorHex), range: range)
} }
case "font": case "font":
let fontSize = json?["size"] as? CGFloat let fontSize = attribute["size"] as? CGFloat
var font: UIFont? var font: UIFont?
if let fontName = attribute.optionalStringForKey("name") { if let fontName = attribute.optionalStringForKey("name") {
font = MFFonts.mfFont(withName: fontName, size: fontSize ?? label.font.pointSize) font = MFFonts.mfFont(withName: fontName, size: fontSize ?? label.font.pointSize)
} else if let fontSize = fontSize { } else if let fontSize = fontSize {
font = label.font.withSize(fontSize) font = label.font.withSize(fontSize)
} }
if font != nil { if let font = font {
attributedString.addAttribute(.font, value: font as Any, range: range) attributedString.removeAttribute(.font, range: range)
attributedString.addAttribute(.font, value: font as UIFont, range: range)
} }
default: default:
continue continue
@ -294,3 +302,7 @@ import MVMCore
originalAttributedString = attributedText originalAttributedString = attributedText
} }
} }
extension Label {
}

View File

@ -62,6 +62,10 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
} }
} }
private var actionRange: NSRange {
return NSRange(location: frontText?.count ?? 0, length: actionText?.count ?? 0)
}
public var makeWholeViewClickable = false public var makeWholeViewClickable = false
override open var isEnabled: Bool { override open var isEnabled: Bool {
@ -289,7 +293,7 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
let location: CGPoint? = touches?.first?.location(in: label) let location: CGPoint? = touches?.first?.location(in: label)
let actionString = actionText let actionString = actionText
let index: Int = getActionRange().location let index: Int = actionRange.location
let rangeArray = getRangeArrayOfWords(in: actionString, withInitalIndex: index) let rangeArray = getRangeArrayOfWords(in: actionString, withInitalIndex: index)
let rectArray = getRectArray(fromRangeArray: rangeArray) let rectArray = getRectArray(fromRangeArray: rangeArray)
var result = false var result = false
@ -327,11 +331,6 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
return "\(frontText ?? "")\(actionText ?? "")\(backText ?? "")" return "\(frontText ?? "")\(actionText ?? "")\(backText ?? "")"
} }
private func getActionRange() -> NSRange {
return NSRange(location: frontText?.count ?? 0, length: actionText?.count ?? 0)
}
private func getRangeArrayOfWords(in string: String?, withInitalIndex index: Int) -> [Any]? { private func getRangeArrayOfWords(in string: String?, withInitalIndex index: Int) -> [Any]? {
var index = index var index = index
@ -446,7 +445,7 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
let attributedString = NSMutableAttributedString(attributedString: thisAttributedText) let attributedString = NSMutableAttributedString(attributedString: thisAttributedText)
if !attributedString.string.isEmpty { if !attributedString.string.isEmpty {
attributedString.addAttributes(theseAttributes, range: getActionRange()) attributedString.addAttributes(theseAttributes, range: actionRange)
} }
attributedText = attributedString attributedText = attributedString
} }