diff --git a/apps/ios/Shared/MyPlayground.playground/Contents.swift b/apps/ios/Shared/MyPlayground.playground/Contents.swift index 16f46301b..1b5484463 100644 --- a/apps/ios/Shared/MyPlayground.playground/Contents.swift +++ b/apps/ios/Shared/MyPlayground.playground/Contents.swift @@ -12,3 +12,15 @@ var a = [1, 2, 3] a.removeAll(where: { $0 == 1} ) print(a) + +let input = "This is a test with the привет 🙂 URL https://www.hackingwithswift.com to be detected." +let detector = try! NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue) +let matches = detector.matches(in: input, options: [], range: NSRange(location: 0, length: input.count)) + +print(matches) + +for match in matches { + guard let range = Range(match.range, in: input) else { continue } + let url = input[range] + print(url) +} diff --git a/apps/ios/Shared/MyPlayground.playground/timeline.xctimeline b/apps/ios/Shared/MyPlayground.playground/timeline.xctimeline index a6df7c391..522d23c91 100644 --- a/apps/ios/Shared/MyPlayground.playground/timeline.xctimeline +++ b/apps/ios/Shared/MyPlayground.playground/timeline.xctimeline @@ -3,7 +3,7 @@ version = "3.0"> diff --git a/apps/ios/Shared/Views/Chat/ChatItem/TextItemView.swift b/apps/ios/Shared/Views/Chat/ChatItem/TextItemView.swift index a9a25b01f..026fc4d74 100644 --- a/apps/ios/Shared/Views/Chat/ChatItem/TextItemView.swift +++ b/apps/ios/Shared/Views/Chat/ChatItem/TextItemView.swift @@ -11,14 +11,14 @@ import SwiftUI struct TextItemView: View { var chatItem: ChatItem var width: CGFloat + private let codeFont = Font.custom("Courier", size: UIFont.preferredFont(forTextStyle: .body).pointSize) var body: some View { let sent = chatItem.chatDir.sent let minWidth = min(200, width) let maxWidth = min(300, width * 0.78) - return VStack { - Text(chatItem.content.text) + messageText(chatItem.content.text, sent: sent) .padding(.top, 8) .padding(.horizontal, 12) .frame(minWidth: minWidth, maxWidth: maxWidth, alignment: .leading) @@ -42,13 +42,55 @@ struct TextItemView: View { alignment: sent ? .trailing : .leading ) } + + private func messageText(_ s: String, sent: Bool = false) -> Text { + if s == "" { return Text("") } + let parts = s.split(separator: " ") + print(parts) + var res = wordToText(parts[0], sent) + var i = 1 + while i < parts.count { + res = res + Text(" ") + wordToText(parts[i], sent) + i = i + 1 + } + return res + } + + private func wordToText(_ s: String.SubSequence, _ sent: Bool) -> Text { + switch true { + case s.starts(with: "http://") || s.starts(with: "https://"): + let str = String(s) + return Text(AttributedString(str, attributes: AttributeContainer([ + .link: NSURL(string: str) as Any, + .foregroundColor: (sent ? UIColor.white : nil) as Any + ]))).underline() + default: + if (s.count > 1) { + switch true { + case s.first == "*" && s.last == "*": return mdText(s).bold() + case s.first == "_" && s.last == "_": return mdText(s).italic() + case s.first == "+" && s.last == "+": return mdText(s).underline() + case s.first == "~" && s.last == "~": return mdText(s).strikethrough() + default: return Text(s) + } + } else { + return Text(s) + } + } + } + + private func mdText(_ s: String.SubSequence) -> Text { + Text(s[s.index(s.startIndex, offsetBy: 1)..