summaryrefslogtreecommitdiff
path: root/Juick/Views/AttributedLabelView.swift
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2020-09-17 00:46:27 +0300
committerGravatar Vitaly Takmazov2020-12-10 18:59:55 +0300
commitcf97b1cd43a91725e0419a953815287fde0bf70f (patch)
tree82961197cf7fa1bfa40550748ddd309e5e4d38c3 /Juick/Views/AttributedLabelView.swift
parent4851a21c41488a6f0c01e60f1bd1472846f816bf (diff)
SwiftUI WIP
Diffstat (limited to 'Juick/Views/AttributedLabelView.swift')
-rw-r--r--Juick/Views/AttributedLabelView.swift107
1 files changed, 107 insertions, 0 deletions
diff --git a/Juick/Views/AttributedLabelView.swift b/Juick/Views/AttributedLabelView.swift
new file mode 100644
index 0000000..08051a9
--- /dev/null
+++ b/Juick/Views/AttributedLabelView.swift
@@ -0,0 +1,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())
+ }
+ }
+}