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
//------------------------------------------------------
@ -114,7 +114,7 @@ open class CaretButton: MFCustomButton {
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)
guard let dictionary = json else { return }

View File

@ -36,6 +36,7 @@ import MVMCore
//------------------------------------------------------
@objc public func setupView() {
backgroundColor = .clear
numberOfLines = 0
lineBreakMode = .byWordWrapping
@ -115,7 +116,7 @@ import MVMCore
}
@objc open class func label() -> Label {
return Label(frame: CGRect.zero)
return Label(frame: .zero)
}
//------------------------------------------------------
@ -128,7 +129,8 @@ import MVMCore
do {
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)
} catch {
if let coreErrorObject = MVMCoreErrorObject.createErrorObject(for: error, location: "LabelHTMLParse") {
@ -142,7 +144,8 @@ import MVMCore
guard let label = label else { return }
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 {
label.textColor = UIColor.mfGet(forHex: textColorHex)
@ -163,37 +166,42 @@ import MVMCore
}
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])
let attributedString = NSMutableAttributedString(string: labelText, attributes: [NSAttributedString.Key.font: label.font as UIFont,
NSAttributedString.Key.foregroundColor: label.textColor as UIColor])
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) else { continue }
guard let attributeType = attribute.optionalStringForKey(KeyType),
let location = attribute["location"] as? Int,
let length = attribute["length"] as? Int
else { continue }
let range = NSRange(location: location, length: length)
switch attributeType {
case "underline":
attributedString.addAttribute(.underlineStyle, value: NSUnderlineStyle.single, range: range)
attributedString.addAttribute(.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: range)
case "strikethrough":
attributedString.addAttribute(.strikethroughStyle, value: NSUnderlineStyle.thick, range: range)
attributedString.addAttribute(.strikethroughStyle, value: NSUnderlineStyle.thick.rawValue, range: range)
case "color":
if let colorHex = attribute.optionalStringForKey(KeyTextColor), !colorHex.isEmpty {
attributedString.removeAttribute(.foregroundColor, range: range)
attributedString.addAttribute(.foregroundColor, value: UIColor.mfGet(forHex: colorHex), range: range)
}
case "font":
let fontSize = json?["size"] as? CGFloat
let fontSize = attribute["size"] as? CGFloat
var font: UIFont?
if let fontName = attribute.optionalStringForKey("name") {
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)
}
if font != nil {
attributedString.addAttribute(.font, value: font as Any, range: range)
if let font = font {
attributedString.removeAttribute(.font, range: range)
attributedString.addAttribute(.font, value: font as UIFont, range: range)
}
default:
continue
@ -294,3 +302,7 @@ import MVMCore
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
override open var isEnabled: Bool {
@ -289,7 +293,7 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
let location: CGPoint? = touches?.first?.location(in: label)
let actionString = actionText
let index: Int = getActionRange().location
let index: Int = actionRange.location
let rangeArray = getRangeArrayOfWords(in: actionString, withInitalIndex: index)
let rectArray = getRectArray(fromRangeArray: rangeArray)
var result = false
@ -327,11 +331,6 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
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]? {
var index = index
@ -446,7 +445,7 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
let attributedString = NSMutableAttributedString(attributedString: thisAttributedText)
if !attributedString.string.isEmpty {
attributedString.addAttributes(theseAttributes, range: getActionRange())
attributedString.addAttributes(theseAttributes, range: actionRange)
}
attributedText = attributedString
}