MessageView.swift 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. //
  2. // MessageView.swift
  3. // Chat
  4. //
  5. // Created by Sergey Tarasov on 24.07.2022.
  6. //
  7. import SwiftUI
  8. struct MessageView: View {
  9. let text: String
  10. let time: String
  11. let isSelf: Bool
  12. var body: some View {
  13. HStack {
  14. if isSelf {
  15. Spacer(minLength: 40)
  16. }
  17. HStack(alignment: .bottom) {
  18. Text(text)
  19. .foregroundColor(.primary)
  20. .padding(.vertical, 6)
  21. .padding(.horizontal, 12)
  22. Text(time)
  23. .font(.caption)
  24. .foregroundColor(.secondary)
  25. .padding(.vertical, 6)
  26. .padding(.trailing, 12)
  27. .padding(.leading, -12)
  28. }
  29. .background(
  30. RoundedRectangle(cornerRadius: 17)
  31. .foregroundColor(isSelf ? .mint.opacity(0.1) : .secondary.opacity(0.1))
  32. )
  33. if !isSelf {
  34. Spacer(minLength: 40)
  35. }
  36. }
  37. }
  38. }
  39. struct MessageView_Previews: PreviewProvider {
  40. static var previews: some View {
  41. MessageTestView()
  42. }
  43. struct MessageTestView: View {
  44. @State var messageText1: String = "Малое пробное сообщение"
  45. @State var messageText2: String = "Пробное сообщение на несколько строк для проверки того, как выглядит сообщение и его фон"
  46. var body: some View {
  47. VStack {
  48. ScrollView {
  49. VStack(spacing: 6) {
  50. MessageView(text: messageText1, time: "23:05", isSelf: true)
  51. MessageView(text: messageText2, time: "23:08", isSelf: false)
  52. }
  53. }
  54. TextField("Message", text: $messageText1)
  55. .textFieldStyle(.roundedBorder)
  56. TextField("Message", text: $messageText2)
  57. .textFieldStyle(.roundedBorder)
  58. }
  59. .padding()
  60. }
  61. }
  62. }