Skip to content

Commit de029e1

Browse files
Added way to customize link attachments text color
1 parent 56efc8c commit de029e1

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed

Sources/StreamChatSwiftUI/ChatChannel/MessageList/LinkAttachmentView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public struct LinkAttachmentContainer<Factory: ViewFactory>: View {
3838
let availableWidth = width - 4 * padding
3939
let size = message.text.frameSize(maxWidth: availableWidth)
4040
LinkTextView(
41-
text: message.text,
41+
message: message,
4242
width: availableWidth,
4343
textColor: UIColor(textColor(for: message))
4444
)

Sources/StreamChatSwiftUI/ChatChannel/MessageList/LinkTextView.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22
// Copyright © 2022 Stream.io Inc. All rights reserved.
33
//
44

5+
import StreamChat
56
import SwiftUI
67
import UIKit
78

89
/// SwiftUI wrapper for displaying links in a text view.
910
struct LinkTextView: UIViewRepresentable {
10-
var text: String
11+
12+
@Injected(\.utils) private var utils
13+
14+
var message: ChatMessage
1115
var width: CGFloat
1216
var textColor: UIColor
1317

@@ -25,6 +29,7 @@ struct LinkTextView: UIViewRepresentable {
2529
textView.adjustsFontForContentSizeCategory = true
2630
textView.text = text
2731
textView.textColor = textColor
32+
textView.linkTextAttributes = utils.messageListConfig.messageDisplayOptions.messageLinkDisplayResolver(message)
2833
return textView
2934
}
3035

@@ -35,6 +40,10 @@ struct LinkTextView: UIViewRepresentable {
3540
uiView.frame.size = size
3641
}
3742
}
43+
44+
private var text: String {
45+
message.text
46+
}
3847
}
3948

4049
/// Text View that ignores all user interactions except touches on links

Sources/StreamChatSwiftUI/ChatChannel/MessageList/MessageListConfig.swift

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,28 +67,40 @@ public enum DateIndicatorPlacement {
6767

6868
/// Used to show and hide different helper views around the message.
6969
public struct MessageDisplayOptions {
70-
70+
7171
let showAvatars: Bool
7272
let showMessageDate: Bool
7373
let showAuthorName: Bool
7474
let animateChanges: Bool
7575
let currentUserMessageTransition: AnyTransition
7676
let otherUserMessageTransition: AnyTransition
77+
var messageLinkDisplayResolver: (ChatMessage) -> [NSAttributedString.Key: Any]
7778

7879
public init(
7980
showAvatars: Bool = true,
8081
showMessageDate: Bool = true,
8182
showAuthorName: Bool = true,
8283
animateChanges: Bool = true,
8384
currentUserMessageTransition: AnyTransition = .identity,
84-
otherUserMessageTransition: AnyTransition = .identity
85+
otherUserMessageTransition: AnyTransition = .identity,
86+
messageLinkDisplayResolver: @escaping (ChatMessage) -> [NSAttributedString.Key: Any] = MessageDisplayOptions
87+
.defaultLinkDisplay
8588
) {
8689
self.showAvatars = showAvatars
8790
self.showAuthorName = showAuthorName
8891
self.showMessageDate = showMessageDate
8992
self.animateChanges = animateChanges
9093
self.currentUserMessageTransition = currentUserMessageTransition
9194
self.otherUserMessageTransition = otherUserMessageTransition
95+
self.messageLinkDisplayResolver = messageLinkDisplayResolver
96+
}
97+
98+
public static var defaultLinkDisplay: (ChatMessage) -> [NSAttributedString.Key: Any] {
99+
{ _ in
100+
[
101+
NSAttributedString.Key.foregroundColor: UIColor(InjectedValues[\.colors].tintColor)
102+
]
103+
}
92104
}
93105
}
94106

docusaurus/docs/iOS/swiftui/components/message-list.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,23 @@ let messageDisplayOptions = MessageDisplayOptions(
4949
)
5050
```
5151

52+
For link attachments, you can control the link text attributes (font, font weight, color) based on the message. Here's an example how to change the link color based on the message sender, with the `messageLinkDisplayResolver`:
53+
54+
```swift
55+
let messageDisplayOptions = MessageDisplayOptions(messageLinkDisplayResolver: { message in
56+
let color = message.isSentByCurrentUser ? UIColor.red : UIColor.green
57+
return [
58+
NSAttributedString.Key.foregroundColor: color
59+
]
60+
})
61+
let messageListConfig = MessageListConfig(messageDisplayOptions: messageDisplayOptions)
62+
let utils = Utils(messageListConfig: messageListConfig)
63+
64+
let streamChat = StreamChat(chatClient: chatClient, utils: utils)
65+
```
66+
67+
## Message List Background
68+
5269
You can also modify the background of the message list to any SwiftUI `View` (`Color`, `LinearGradient`, `Image` etc.). In order to do this, you would need to implement the `makeMessageListBackground` method in the `ViewFactory`.
5370

5471
```swift

0 commit comments

Comments
 (0)