MessageStyle.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. MIT License
  3. Copyright (c) 2017-2018 MessageKit
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. */
  20. import UIKit
  21. public enum MessageStyle {
  22. // MARK: - TailCorner
  23. public enum TailCorner: String {
  24. case topLeft
  25. case bottomLeft
  26. case topRight
  27. case bottomRight
  28. internal var imageOrientation: UIImage.Orientation {
  29. switch self {
  30. case .bottomRight: return .up
  31. case .bottomLeft: return .upMirrored
  32. case .topLeft: return .down
  33. case .topRight: return .downMirrored
  34. }
  35. }
  36. }
  37. // MARK: - TailStyle
  38. public enum TailStyle {
  39. case curved
  40. case pointedEdge
  41. internal var imageNameSuffix: String {
  42. switch self {
  43. case .curved:
  44. return "_tail_v2"
  45. case .pointedEdge:
  46. return "_tail_v1"
  47. }
  48. }
  49. }
  50. // MARK: - MessageStyle
  51. case none
  52. case bubble
  53. case bubbleOutline(UIColor)
  54. case bubbleTail(TailCorner, TailStyle)
  55. case bubbleTailOutline(UIColor, TailCorner, TailStyle)
  56. case custom((MessageContainerView) -> Void)
  57. // MARK: - Public
  58. public var image: UIImage? {
  59. guard let imageCacheKey = imageCacheKey, let path = imagePath else { return nil }
  60. let cache = MessageStyle.bubbleImageCache
  61. if let cachedImage = cache.object(forKey: imageCacheKey as NSString) {
  62. return cachedImage
  63. }
  64. guard var image = UIImage(contentsOfFile: path) else { return nil }
  65. switch self {
  66. case .none, .custom:
  67. return nil
  68. case .bubble, .bubbleOutline:
  69. break
  70. case .bubbleTail(let corner, _), .bubbleTailOutline(_, let corner, _):
  71. guard let cgImage = image.cgImage else { return nil }
  72. image = UIImage(cgImage: cgImage, scale: image.scale, orientation: corner.imageOrientation)
  73. }
  74. let stretchedImage = stretch(image)
  75. cache.setObject(stretchedImage, forKey: imageCacheKey as NSString)
  76. return stretchedImage
  77. }
  78. // MARK: - Internal
  79. internal static let bubbleImageCache: NSCache<NSString, UIImage> = {
  80. let cache = NSCache<NSString, UIImage>()
  81. cache.name = "com.messagekit.MessageKit.bubbleImageCache"
  82. return cache
  83. }()
  84. // MARK: - Private
  85. private var imageCacheKey: String? {
  86. guard let imageName = imageName else { return nil }
  87. switch self {
  88. case .bubble, .bubbleOutline:
  89. return imageName
  90. case .bubbleTail(let corner, _), .bubbleTailOutline(_, let corner, _):
  91. return imageName + "_" + corner.rawValue
  92. default:
  93. return nil
  94. }
  95. }
  96. private var imageName: String? {
  97. switch self {
  98. case .bubble:
  99. return "bubble_full"
  100. case .bubbleOutline:
  101. return "bubble_outlined"
  102. case .bubbleTail(_, let tailStyle):
  103. return "bubble_full" + tailStyle.imageNameSuffix
  104. case .bubbleTailOutline(_, _, let tailStyle):
  105. return "bubble_outlined" + tailStyle.imageNameSuffix
  106. case .none, .custom:
  107. return nil
  108. }
  109. }
  110. private var imagePath: String? {
  111. guard let imageName = imageName else { return nil }
  112. let assetBundle = Bundle.messageKitAssetBundle()
  113. return assetBundle.path(forResource: imageName, ofType: "png", inDirectory: "Images")
  114. }
  115. private func stretch(_ image: UIImage) -> UIImage {
  116. let center = CGPoint(x: image.size.width / 2, y: image.size.height / 2)
  117. let capInsets = UIEdgeInsets(top: center.y, left: center.x, bottom: center.y, right: center.x)
  118. return image.resizableImage(withCapInsets: capInsets, resizingMode: .stretch)
  119. }
  120. }