Proyectos de Subversion Iphone Microlearning - Inconcert

Rev

| 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
struct TextHtmlView : UIViewRepresentable {
17
 
18
    let baseFontSize: CGFloat = 16
19
    typealias UIViewType = UITextView
20
 
21
    private var content : String
22
 
23
    public init(content: String) {
24
        self.content = content
25
    }
26
 
27
 
28
    func makeUIView(context: Context) -> UITextView {
29
 
30
        let textView = UITextView()
31
        textView.backgroundColor = UIColor(Color("color_window_background"))
32
 
33
        let textHtml = content.htmlUnescape()
34
        textView.attributedText =  process(s: textHtml)
35
 
36
        return textView
37
    }
38
 
39
 
40
    private func process(s : String) -> NSAttributedString {
41
 
42
        let headerStyle = Style {
43
            $0.font = UIFont.boldSystemFont(ofSize: self.baseFontSize * 1.15)
44
            $0.lineSpacing = 1
45
            $0.kerning = Kerning.adobe(-20)
46
        }
47
        let boldStyle = Style {
48
            $0.font = UIFont.boldSystemFont(ofSize: self.baseFontSize )
49
 
50
        }
51
 
52
        let h1Style = Style {
53
            $0.font = UIFont.boldSystemFont(ofSize: self.baseFontSize * 1.40)
54
 
55
        }
56
 
57
        let h2Style = Style {
58
            $0.font = UIFont.boldSystemFont(ofSize: self.baseFontSize * 1.30)
59
 
60
        }
61
 
62
        let italicStyle = Style {
63
            $0.font = UIFont.italicSystemFont(ofSize: self.baseFontSize)
64
        }
65
 
66
        let uppercased = Style {
67
            $0.font = UIFont.italicSystemFont(ofSize: self.baseFontSize)
68
            $0.textTransforms = [
69
                .uppercase
70
            ]
71
        }
72
 
73
        let styleGroup = StyleGroup(base: Style {
74
            $0.font = UIFont.systemFont(ofSize: self.baseFontSize)
75
            $0.lineSpacing = 2
76
            $0.kerning = Kerning.adobe(-15)
77
        }, [
78
            "ur": uppercased,
79
            "h1": h1Style,
80
            "h2": h2Style,
81
            "h3": headerStyle,
82
            "h4": headerStyle,
83
            "h5": headerStyle,
84
            "strong": boldStyle,
85
            "b": boldStyle,
86
            "em": italicStyle,
87
            "i": italicStyle,
88
            "a": uppercased,
89
            "li": Style {
90
                $0.paragraphSpacingBefore = self.baseFontSize / 2
91
                $0.firstLineHeadIndent = self.baseFontSize
92
                $0.headIndent = self.baseFontSize * 1.71
93
            },
94
            "sup": Style {
95
                $0.font = UIFont.systemFont(ofSize: self.baseFontSize / 1.2)
96
                $0.baselineOffset = Float(self.baseFontSize) / 3.5
97
            }
98
         ])
99
 
100
        return s.set(style: styleGroup)
101
    }
102
 
103
    func updateUIView(_ uiView: UITextView, context: Context) {
104
 
105
        let textHtml = content.htmlUnescape()
106
        uiView.attributedText = process(s: textHtml)
107
    }
108
}