@@ -4,25 +4,41 @@ import UIKit
44class MarkFormatter : AttributeFormatter {
55
66 var placeholderAttributes : [ NSAttributedString . Key : Any ] ?
7-
7+ var textColor : String ?
8+ var defaulTextColor : UIColor ?
9+
810 func applicationRange( for range: NSRange , in text: NSAttributedString ) -> NSRange {
911 return range
1012 }
1113
1214 func apply( to attributes: [ NSAttributedString . Key : Any ] , andStore representation: HTMLRepresentation ? ) -> [ NSAttributedString . Key : Any ] {
13- var resultingAttributes = attributes
15+ var resultingAttributes = attributes
16+ let colorStyle = CSSAttribute ( name: " color " , value: textColor)
17+ let backgroundColorStyle = CSSAttribute ( name: " background-color " , value: " rgba(0, 0, 0, 0) " )
18+
19+ let styleAttribute = Attribute ( type: . style, value: . inlineCss( [ backgroundColorStyle, colorStyle] ) )
20+ let classAttribute = Attribute ( type: . class, value: . string( " has-inline-color " ) )
1421
15- var representationToUse = HTMLRepresentation ( for: . element( HTMLElementRepresentation . init ( name: " mark " , attributes: [ ] ) ) )
22+ // Setting the HTML representation
23+ var representationToUse = HTMLRepresentation ( for: . element( HTMLElementRepresentation . init ( name: " mark " , attributes: [ styleAttribute, classAttribute] ) ) )
1624 if let requestedRepresentation = representation {
1725 representationToUse = requestedRepresentation
1826 }
1927 resultingAttributes [ . markHtmlRepresentation] = representationToUse
28+
29+ if ( textColor != nil ) {
30+ resultingAttributes [ . foregroundColor] = UIColor ( hexString: textColor!)
31+ }
2032
2133 return resultingAttributes
2234 }
2335
2436 func remove( from attributes: [ NSAttributedString . Key : Any ] ) -> [ NSAttributedString . Key : Any ] {
2537 var resultingAttributes = attributes
38+
39+ if ( defaulTextColor != nil ) {
40+ resultingAttributes [ . foregroundColor] = defaulTextColor
41+ }
2642
2743 resultingAttributes. removeValue ( forKey: . markHtmlRepresentation)
2844 return resultingAttributes
0 commit comments