Proyectos de Subversion Iphone Microlearning - Nuevo Interface

Rev

Rev 7 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 7 Rev 8
Línea 11... Línea 11...
11
import HTMLEntities
11
import HTMLEntities
12
import SwiftRichString
12
import SwiftRichString
Línea 13... Línea 13...
13
 
13
 
Línea 14... Línea -...
14
typealias TextHtml = UIViewRepresentable
-
 
15
 
-
 
16
 
-
 
17
 
-
 
18
 
-
 
19
 
14
typealias TextHtml = UIViewRepresentable
Línea 20... Línea 15...
20
 
15
 
21
struct TextHtmlView : UIViewRepresentable {
16
struct TextHtmlView : UIViewRepresentable {
Línea 37... Línea 32...
37
        
32
        
38
        let textHtml = content.htmlUnescape()
33
        let textHtml = content.htmlUnescape()
Línea 39... Línea 34...
39
        textView.attributedText =  process(s: textHtml)
34
        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
        
35
 
Línea 57... Línea 36...
57
        return textView*/
36
        return textView
58
    }
37
    }
59
 
-
 
60
    
-
 
61
    private func process(s : String) -> NSAttributedString {
-
 
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>"
38
 
80
        return str.set(style: myGroup)
39
    
81
        */
40
    private func process(s : String) -> NSAttributedString {
82
        
41
       
83
        let headerStyle = Style {
42
        let headerStyle = Style {
Línea 109... Línea 68...
109
            $0.textTransforms = [
68
            $0.textTransforms = [
110
                .uppercase
69
                .uppercase
111
            ]
70
            ]
112
        }
71
        }
Línea 113... Línea -...
113
                        
-
 
114
                // And a group of them
72
                        
115
        let styleGroup = StyleGroup(base: Style {
73
        let styleGroup = StyleGroup(base: Style {
116
            $0.font = UIFont.systemFont(ofSize: self.baseFontSize)
74
            $0.font = UIFont.systemFont(ofSize: self.baseFontSize)
117
            $0.lineSpacing = 2
75
            $0.lineSpacing = 2
118
            $0.kerning = Kerning.adobe(-15)
76
            $0.kerning = Kerning.adobe(-15)
Línea 137... Línea 95...
137
                $0.font = UIFont.systemFont(ofSize: self.baseFontSize / 1.2)
95
                $0.font = UIFont.systemFont(ofSize: self.baseFontSize / 1.2)
138
                $0.baselineOffset = Float(self.baseFontSize) / 3.5
96
                $0.baselineOffset = Float(self.baseFontSize) / 3.5
139
            }
97
            }
140
         ])
98
         ])
Línea 141... Línea -...
141
 
-
 
142
            // Apply a custom xml attribute resolver
-
 
143
        //styleGroup.xmlAttributesResolver = MyXMLDynamicAttributesResolver()
99
 
144
        return s.set(style: styleGroup)
100
        return s.set(style: styleGroup)
Línea 145... Línea 101...
145
    }
101
    }
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
 
-
 
Línea 167... Línea -...
167
    
-
 
168
}
-
 
169
 
-
 
170
/*
-
 
171
extension String {
-
 
172
 
-
 
173
 var htmlToAttributedString: NSMutableAttributedString? {
102
     
174
    guard let data = data(using: .utf8) else { return nil }
-
 
175
    do {
-
 
176
        return try NSMutableAttributedString(data: data,
103
    func updateUIView(_ uiView: UITextView, context: Context) {
177
                                      options: [.documentType: NSMutableAttributedString.DocumentType.html,
-
 
178
                                                .characterEncoding: String.Encoding.utf8.rawValue],
-
 
179
                                      documentAttributes: nil)
-
 
180
    } catch let error as NSError {
104
 
181
        print(error.localizedDescription)
-
 
182
        return  nil
-
 
183
    }
105
        let textHtml = content.htmlUnescape()
184
 }
-
 
185
 
-
 
186
}
-
 
187
 */
-
 
188
 
-
 
189
/*
-
 
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
        
-