Proyectos de Subversion Iphone Microlearning - Inconcert

Rev

Autoría | Ultima modificación | Ver Log |

//
//  TextHtml.swift
//  twogetskills
//
//  Created by Efrain Yanez Recanatini on 7/30/22.
//

import Foundation
import SwiftUI
import WebKit
import HTMLEntities
import SwiftRichString

typealias TextHtml = UIViewRepresentable

struct TextHtmlView : UIViewRepresentable {

    let baseFontSize: CGFloat = 16
    typealias UIViewType = UITextView
    
    private var content : String
    
    public init(content: String) {
        self.content = content
    }

    
    func makeUIView(context: Context) -> UITextView {
        
        let textView = UITextView()
        textView.backgroundColor = UIColor(Color("color_window_background"))
        
        let textHtml = content.htmlUnescape()
        textView.attributedText =  process(s: textHtml)

        return textView
    }

    
    private func process(s : String) -> NSAttributedString {
       
        let headerStyle = Style {
            $0.font = UIFont.boldSystemFont(ofSize: self.baseFontSize * 1.15)
            $0.lineSpacing = 1
            $0.kerning = Kerning.adobe(-20)
        }
        let boldStyle = Style {
            $0.font = UIFont.boldSystemFont(ofSize: self.baseFontSize )
           
        }
        
        let h1Style = Style {
            $0.font = UIFont.boldSystemFont(ofSize: self.baseFontSize * 1.40)

        }
        
        let h2Style = Style {
            $0.font = UIFont.boldSystemFont(ofSize: self.baseFontSize * 1.30)

        }

        let italicStyle = Style {
            $0.font = UIFont.italicSystemFont(ofSize: self.baseFontSize)
        }
                
        let uppercased = Style {
            $0.font = UIFont.italicSystemFont(ofSize: self.baseFontSize)
            $0.textTransforms = [
                .uppercase
            ]
        }
                        
        let styleGroup = StyleGroup(base: Style {
            $0.font = UIFont.systemFont(ofSize: self.baseFontSize)
            $0.lineSpacing = 2
            $0.kerning = Kerning.adobe(-15)
        }, [
            "ur": uppercased,
            "h1": h1Style,
            "h2": h2Style,
            "h3": headerStyle,
            "h4": headerStyle,
            "h5": headerStyle,
            "strong": boldStyle,
            "b": boldStyle,
            "em": italicStyle,
            "i": italicStyle,
            "a": uppercased,
            "li": Style {
                $0.paragraphSpacingBefore = self.baseFontSize / 2
                $0.firstLineHeadIndent = self.baseFontSize
                $0.headIndent = self.baseFontSize * 1.71
            },
            "sup": Style {
                $0.font = UIFont.systemFont(ofSize: self.baseFontSize / 1.2)
                $0.baselineOffset = Float(self.baseFontSize) / 3.5
            }
         ])

        return s.set(style: styleGroup)
    }
     
    func updateUIView(_ uiView: UITextView, context: Context) {

        let textHtml = content.htmlUnescape()
        uiView.attributedText = process(s: textHtml)
    }
}