ios: highlight URLs in texts (#272)
* ios: highlight URLs in texts * Apply suggestions from code review
This commit is contained in:
parent
67dbdcd257
commit
5aabf87898
@ -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)
|
||||
}
|
||||
|
@ -3,7 +3,7 @@
|
||||
version = "3.0">
|
||||
<TimelineItems>
|
||||
<LoggerValueHistoryTimelineItem
|
||||
documentLocation = "file:///Users/evgeny/opensource/simplex-chat/simplex-chat/apps/ios/Shared/MyPlayground.playground#CharacterRangeLen=88&CharacterRangeLoc=91&EndingColumnNumber=0&EndingLineNumber=7&StartingColumnNumber=3&StartingLineNumber=6&Timestamp=665423482.97412"
|
||||
documentLocation = "file:///Users/evgeny/opensource/simplex-chat/simplex-chat/apps/ios/Shared/MyPlayground.playground#CharacterRangeLen=88&CharacterRangeLoc=91&EndingColumnNumber=0&EndingLineNumber=7&StartingColumnNumber=3&StartingLineNumber=6&Timestamp=665788560.47885"
|
||||
selectedRepresentationIndex = "0"
|
||||
shouldTrackSuperviewWidth = "NO">
|
||||
</LoggerValueHistoryTimelineItem>
|
||||
|
@ -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)..<s.index(s.endIndex, offsetBy: -1)])
|
||||
}
|
||||
}
|
||||
|
||||
struct TextItemView_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
Group{
|
||||
TextItemView(chatItem: chatItemSample(1, .directSnd, .now, "hello"), width: 360)
|
||||
TextItemView(chatItem: chatItemSample(2, .directSnd, .now, "https://simplex.chat"), width: 360)
|
||||
TextItemView(chatItem: chatItemSample(2, .directRcv, .now, "hello there too"), width: 360)
|
||||
TextItemView(chatItem: chatItemSample(2, .directRcv, .now, "https://simplex.chat"), width: 360)
|
||||
}
|
||||
.previewLayout(.fixed(width: 360, height: 70))
|
||||
}
|
||||
|
@ -10,6 +10,8 @@ import SwiftUI
|
||||
|
||||
struct ChatPreviewView: View {
|
||||
@ObservedObject var chat: Chat
|
||||
@Environment(\.colorScheme) var colorScheme
|
||||
var darkGreen = Color(red: 0, green: 0.5, blue: 0)
|
||||
|
||||
var body: some View {
|
||||
let cItem = chat.chatItems.last
|
||||
@ -21,7 +23,7 @@ struct ChatPreviewView: View {
|
||||
chat.serverInfo.networkStatus == .connected {
|
||||
Image(systemName: "circle.fill")
|
||||
.resizable()
|
||||
.foregroundColor(.green)
|
||||
.foregroundColor(colorScheme == .dark ? darkGreen : .green)
|
||||
.frame(width: 5, height: 5)
|
||||
.padding([.bottom, .leading], 1)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user