2022-12-29 08:06:21 +01:00
//
// h t t p s : / / m c z a c h u r s k i . d e v
// C o p y r i g h t © 2 0 2 2 M a r c i n C z a c h u r s k i a n d t h e r e p o s i t o r y c o n t r i b u t o r s .
// L i c e n s e d u n d e r t h e M I T L i c e n s e .
//
import UIKit
import SwiftUI
struct HTMLFormattedText : UIViewRepresentable {
let text : String
private let textView = UITextView ( )
2023-01-03 14:09:22 +01:00
private let fontSize : Int
private let width : Int
2022-12-29 08:06:21 +01:00
2023-01-03 14:09:22 +01:00
init ( _ content : String , withFontSize fontSize : Int = 16 , andWidth width : Int ? = nil ) {
2022-12-29 08:06:21 +01:00
self . text = content
2023-01-03 14:09:22 +01:00
self . fontSize = fontSize
self . width = width ? ? Int ( UIScreen . main . bounds . width ) - 16
2022-12-29 08:06:21 +01:00
}
func makeUIView ( context : UIViewRepresentableContext < Self > ) -> UITextView {
2023-01-03 14:09:22 +01:00
textView . widthAnchor . constraint ( equalToConstant : CGFloat ( self . width ) ) . isActive = true
2022-12-29 08:06:21 +01:00
textView . isSelectable = false
textView . isUserInteractionEnabled = false
textView . translatesAutoresizingMaskIntoConstraints = false
textView . isScrollEnabled = false
2023-01-04 17:56:01 +01:00
textView . backgroundColor = UIColor ( Color . clear )
2022-12-29 08:06:21 +01:00
return textView
}
func updateUIView ( _ uiView : UITextView , context : UIViewRepresentableContext < Self > ) {
DispatchQueue . main . async {
if let attributeText = self . converHTML ( text : text ) {
textView . attributedText = attributeText
} else {
textView . text = " "
}
}
}
private func converHTML ( text : String ) -> NSAttributedString ? {
2022-12-31 16:31:05 +01:00
guard let data = text . data ( using : . utf16 ) else {
2022-12-29 08:06:21 +01:00
return nil
}
let largeAttributes = [
2023-01-03 14:09:22 +01:00
NSAttributedString . Key . font : UIFont . systemFont ( ofSize : CGFloat ( self . fontSize ) ) ,
2023-01-04 17:56:01 +01:00
NSAttributedString . Key . foregroundColor : UIColor ( Color ( " MainTextColor " ) )
2022-12-29 08:06:21 +01:00
]
let linkAttributes = [
2023-01-03 14:09:22 +01:00
NSAttributedString . Key . font : UIFont . systemFont ( ofSize : CGFloat ( self . fontSize ) ) ,
2023-01-04 17:56:01 +01:00
NSAttributedString . Key . foregroundColor : UIColor ( Color . accentColor )
2022-12-29 08:06:21 +01:00
]
if let attributedString = try ? NSMutableAttributedString ( data : data , options : [ . documentType : NSAttributedString . DocumentType . html ] , documentAttributes : nil ) {
attributedString . enumerateAttributes ( in : NSRange ( 0. . < attributedString . length ) ) { value , range , stop in
attributedString . setAttributes ( largeAttributes , range : range )
if value . keys . contains ( NSAttributedString . Key . link ) {
attributedString . setAttributes ( linkAttributes , range : range )
}
}
return attributedString
} else {
return nil
}
}
}
struct HTMLFotmattedText_Previews : PreviewProvider {
static var previews : some View {
HTMLFormattedText ( " <p>Danish-made 1st class kebab</p><p>Say yes thanks to 2kg. delicious kebab, which is confused and cooked.</p><p>Yes thanks for 149.95</p><p>Now you can make the most delicious sandwiches, kebab mix and much more at home</p> " )
}
}