Proyectos de Subversion Iphone Microlearning - Nuevo Interface

Rev

Rev 6 | Ir a la última revisión | Autoría | Comparar con el anterior | 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
       
        
        /*
        let htmlData = NSString(string: textHtml).data(using: String.Encoding.utf8.rawValue)
        
        let options = [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html]

        
        let attributedString = try! NSAttributedString(data: htmlData!,
                                                       options: options,
                                                       documentAttributes: nil)
        */
       /*
        textView.text = textHtml
        
        return textView*/
    }

    
    private func process(s : String) -> AttributedString {
        
        /*
        let normal = Style {
            $0.font = SystemFonts.Helvetica_Light.font(size: 15)
        }
                
        let bold = Style {
            $0.font = SystemFonts.Helvetica_Bold.font(size: 20)
            $0.color = UIColor.red
            $0.backColor = UIColor.yellow
        }
                
        let italic = normal.byAdding {
            $0.traitVariants = .italic
        }

        let myGroup = StyleXML(base: normal, ["bold": bold, "italic": italic])
        let str = "Hello <bold>Daniele!</bold>. You're ready to <italic>play with us!</italic>"
        return str.set(style: myGroup)
        */
        
        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
            ]
        }
                        
                // And a group of them
        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
            }
         ])

            // Apply a custom xml attribute resolver
        styleGroup.xmlAttributesResolver = MyXMLDynamicAttributesResolver()
        return s.set(style: styleGroup)
    }
     
     func updateUIView(_ uiView: UITextView, context: Context) {
        
        
        /*
        let htmlData = NSString(string: textHtml).data(using: String.Encoding.utf8.rawValue)
        
        let options = [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html]
        
        let attributedString = try! NSAttributedString(data: htmlData!,
                                                       options: options,
                                                       documentAttributes: nil)
        */
        let textHtml = content.htmlUnescape()
       
            
            
            uiView.attributedText = process(s: textHtml)

     }

    
}

/*
extension String {

 var htmlToAttributedString: NSMutableAttributedString? {
    guard let data = data(using: .utf8) else { return nil }
    do {
        return try NSMutableAttributedString(data: data,
                                      options: [.documentType: NSMutableAttributedString.DocumentType.html,
                                                .characterEncoding: String.Encoding.utf8.rawValue],
                                      documentAttributes: nil)
    } catch let error as NSError {
        print(error.localizedDescription)
        return  nil
    }
 }

}
 */


public class MyXMLDynamicAttributesResolver: StandardXMLAttributesResolver {
    
    public override func styleForUnknownXMLTag(_ tag: String, to attributedString: inout AttributedString, attributes: [String : String]?, fromStyle forStyle: StyleXML) {
        super.styleForUnknownXMLTag(tag, to: &attributedString, attributes: attributes, fromStyle: forStyle)
        
       
        
    }
    
}