summaryrefslogtreecommitdiff
path: root/Juick/Views/AttributedLabelView.swift
blob: 08051a9cc53faa91caeb0cb42e7026b8d98f86a7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//
//  AttributedLabelView.swift
//  Demo
//
//  Created by Ernesto Rivera on 5/14/20.
//  Copyright © 2020 Atributika. All rights reserved.
//
import SwiftUI
import Atributika

@available(iOS 13.0, *)
struct AttributedLabelView: UIViewRepresentable
{
    var attributedText: AttributedText?
    var configureLabel: ((AttributedLabel) -> Void)? = nil
    
    @State var maxWidth: CGFloat = 300
    
    typealias UIViewType = MaxWidthAttributedLabel
    
    func makeUIView(context: Context) -> MaxWidthAttributedLabel
    {
        let view = MaxWidthAttributedLabel()
        configureLabel?(view)
        return view
    }
    
    func updateUIView(_ uiView: MaxWidthAttributedLabel, context: Context)
    {
        uiView.attributedText = attributedText
        uiView.maxWidth = maxWidth
    }
    
    class MaxWidthAttributedLabel: AttributedLabel
    {
        var maxWidth: CGFloat!
        
        open override var intrinsicContentSize: CGSize
        {
            sizeThatFits(CGSize(width: maxWidth, height: .infinity))
        }
    }
}

@available(iOS 13.0, *)
struct AttributtedLabelView_Previews: PreviewProvider
{
    static var previews: some View
    {
        let all = Style.font(UIFont.preferredFont(forTextStyle: .body))
        let link = Style("a")
            .foregroundColor(Color(named: "Title") ?? .blue, .normal)
            .foregroundColor(.brown, .highlighted)
        let configureLabel: ((AttributedLabel) -> Void) = { label in
            label.numberOfLines = 0
            label.textColor = .label
        }
        
        return GeometryReader { geometry in
            List {
                AttributedLabelView(attributedText: """
Denny JA: Dengan RT ini, anda ikut memenangkan Jokowi-JK. Pilih hghghg
                    hghfghfgh
                    fghfgh
                    fgh
                    gfh
                    fgh
                    dipercaya (Jokowi) dan pengalaman (JK). #DJoJK
"""
                    .style(tags: link)
                    .styleHashtags(link)
                    .styleMentions(link)
                    .styleLinks(link)
                    .styleAll(all), configureLabel: configureLabel, maxWidth: geometry.size.width)
                    .fixedSize(horizontal: true, vertical: true)
                    .padding(EdgeInsets(top: 12, leading: 12, bottom: 12, trailing: 24))
                    .listRowInsets(EdgeInsets())
                AttributedLabelView(attributedText: "@e2F If only Bradley's arm was longer. Best ever. 😊 #oscars https://m/p<br>Check this <a href=\"https://github.com/psharanda/Atributika\">link</a>"
                    .style(tags: link)
                    .styleHashtags(link)
                    .styleMentions(link)
                    .styleLinks(link)
                    .styleAll(all), configureLabel: configureLabel)
                    .fixedSize(horizontal: true, vertical: true)
                    .padding()
                    .listRowInsets(EdgeInsets())
                AttributedLabelView(attributedText: """
                # A big message
                - With *mentions* [Ernesto Test Account](user://91010061)
                - **Bold** text
                ## Also
                > Quotes
                1. Some `code`
                2. And data detectors  (801) 917 4444, email@dot.com and http://apple.com
                """
                    .style(tags: link)
                    .styleHashtags(link)
                    .styleMentions(link)
                    .styleLinks(link)
                    .styleAll(all), configureLabel: configureLabel)
                    .fixedSize(horizontal: true, vertical: true)
                    .listRowInsets(EdgeInsets())
                    .padding()
            }.listRowInsets(EdgeInsets())
        }
    }
}