Proyectos de Subversion Iphone Microlearning - Nuevo Interface

Rev

Rev 5 | Rev 7 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
//
2
//  TextHtml.swift
3
//  twogetskills
4
//
5
//  Created by Efrain Yanez Recanatini on 7/30/22.
6
//
7
 
8
import Foundation
9
import SwiftUI
10
import WebKit
11
import HTMLEntities
12
import SwiftRichString
13
 
14
typealias TextHtml = UIViewRepresentable
15
 
16
 
17
 
18
 
19
 
20
 
21
struct TextHtmlView : UIViewRepresentable {
22
 
23
    let baseFontSize: CGFloat = 16
24
    typealias UIViewType = UITextView
25
 
26
    private var content : String
27
 
28
    public init(content: String) {
29
        self.content = content
30
    }
31
 
32
 
33
    func makeUIView(context: Context) -> UITextView {
34
 
35
        let textView = UITextView()
36
        textView.backgroundColor = UIColor(Color("color_window_background"))
37
 
38
        let textHtml = content.htmlUnescape()
39
        textView.attributedText =  process(s: textHtml)
40
 
41
        return textView
42
 
43
 
44
        /*
45
        let htmlData = NSString(string: textHtml).data(using: String.Encoding.utf8.rawValue)
46
 
47
        let options = [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html]
48
 
49
 
50
        let attributedString = try! NSAttributedString(data: htmlData!,
51
                                                       options: options,
52
                                                       documentAttributes: nil)
53
        */
54
       /*
55
        textView.text = textHtml
56
 
57
        return textView*/
58
    }
59
 
60
 
5 efrain 61
    private func process(s : String) -> NSAttributedString {
1 efrain 62
 
63
        /*
64
        let normal = Style {
65
            $0.font = SystemFonts.Helvetica_Light.font(size: 15)
66
        }
67
 
68
        let bold = Style {
69
            $0.font = SystemFonts.Helvetica_Bold.font(size: 20)
70
            $0.color = UIColor.red
71
            $0.backColor = UIColor.yellow
72
        }
73
 
74
        let italic = normal.byAdding {
75
            $0.traitVariants = .italic
76
        }
77
 
78
        let myGroup = StyleXML(base: normal, ["bold": bold, "italic": italic])
79
        let str = "Hello <bold>Daniele!</bold>. You're ready to <italic>play with us!</italic>"
80
        return str.set(style: myGroup)
81
        */
82
 
83
        let headerStyle = Style {
84
            $0.font = UIFont.boldSystemFont(ofSize: self.baseFontSize * 1.15)
85
            $0.lineSpacing = 1
86
            $0.kerning = Kerning.adobe(-20)
87
        }
88
        let boldStyle = Style {
89
            $0.font = UIFont.boldSystemFont(ofSize: self.baseFontSize )
90
 
91
        }
92
 
93
        let h1Style = Style {
94
            $0.font = UIFont.boldSystemFont(ofSize: self.baseFontSize * 1.40)
95
 
96
        }
97
 
98
        let h2Style = Style {
99
            $0.font = UIFont.boldSystemFont(ofSize: self.baseFontSize * 1.30)
100
 
101
        }
102
 
103
        let italicStyle = Style {
104
            $0.font = UIFont.italicSystemFont(ofSize: self.baseFontSize)
105
        }
106
 
107
        let uppercased = Style {
108
            $0.font = UIFont.italicSystemFont(ofSize: self.baseFontSize)
109
            $0.textTransforms = [
110
                .uppercase
111
            ]
112
        }
113
 
114
                // And a group of them
115
        let styleGroup = StyleGroup(base: Style {
116
            $0.font = UIFont.systemFont(ofSize: self.baseFontSize)
117
            $0.lineSpacing = 2
118
            $0.kerning = Kerning.adobe(-15)
119
        }, [
120
            "ur": uppercased,
121
            "h1": h1Style,
122
            "h2": h2Style,
123
            "h3": headerStyle,
124
            "h4": headerStyle,
125
            "h5": headerStyle,
126
            "strong": boldStyle,
127
            "b": boldStyle,
128
            "em": italicStyle,
129
            "i": italicStyle,
130
            "a": uppercased,
131
            "li": Style {
132
                $0.paragraphSpacingBefore = self.baseFontSize / 2
133
                $0.firstLineHeadIndent = self.baseFontSize
134
                $0.headIndent = self.baseFontSize * 1.71
135
            },
136
            "sup": Style {
137
                $0.font = UIFont.systemFont(ofSize: self.baseFontSize / 1.2)
138
                $0.baselineOffset = Float(self.baseFontSize) / 3.5
139
            }
140
         ])
141
 
142
            // Apply a custom xml attribute resolver
143
        styleGroup.xmlAttributesResolver = MyXMLDynamicAttributesResolver()
144
        return s.set(style: styleGroup)
145
    }
146
 
147
     func updateUIView(_ uiView: UITextView, context: Context) {
148
 
149
 
150
        /*
151
        let htmlData = NSString(string: textHtml).data(using: String.Encoding.utf8.rawValue)
152
 
153
        let options = [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html]
154
 
155
        let attributedString = try! NSAttributedString(data: htmlData!,
156
                                                       options: options,
157
                                                       documentAttributes: nil)
158
        */
159
        let textHtml = content.htmlUnescape()
160
 
161
 
162
 
163
            uiView.attributedText = process(s: textHtml)
164
 
165
     }
166
 
167
 
168
}
169
 
170
/*
171
extension String {
172
 
173
 var htmlToAttributedString: NSMutableAttributedString? {
174
    guard let data = data(using: .utf8) else { return nil }
175
    do {
176
        return try NSMutableAttributedString(data: data,
177
                                      options: [.documentType: NSMutableAttributedString.DocumentType.html,
178
                                                .characterEncoding: String.Encoding.utf8.rawValue],
179
                                      documentAttributes: nil)
180
    } catch let error as NSError {
181
        print(error.localizedDescription)
182
        return  nil
183
    }
184
 }
185
 
186
}
187
 */
188
 
6 efrain 189
/*
1 efrain 190
public class MyXMLDynamicAttributesResolver: StandardXMLAttributesResolver {
191
 
192
    public override func styleForUnknownXMLTag(_ tag: String, to attributedString: inout AttributedString, attributes: [String : String]?, fromStyle forStyle: StyleXML) {
193
        super.styleForUnknownXMLTag(tag, to: &attributedString, attributes: attributes, fromStyle: forStyle)
194
 
195
 
196
 
197
    }
198
 
6 efrain 199
}*/