JCCEmoticonLarge.swift 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. //
  2. // JCCEmoticonLarge.swift
  3. // JChat
  4. //
  5. // Created by deng on 2017/3/9.
  6. // Copyright © 2017年 HXHG. All rights reserved.
  7. //
  8. import UIKit
  9. open class JCCEmoticonLarge: JCCEmoticon {
  10. open override func draw(in rect: CGRect, in ctx: CGContext) {
  11. guard let image = contents as? UIImage else {
  12. return
  13. }
  14. var nframe1 = rect.inset(by: UIEdgeInsets(top: 0, left: 0, bottom: 20, right: 0))
  15. var nframe2 = rect.inset(by: UIEdgeInsets(top: nframe1.height, left: 0, bottom: 0, right: 0))
  16. // 图标
  17. let scale = min(min(nframe1.width / image.size.width, nframe1.height / image.size.height), 1)
  18. let imageSize = CGSize(width: image.size.width * scale, height: image.size.height * scale)
  19. nframe1.origin.x = nframe1.minX + (nframe1.width - imageSize.width) / 2
  20. nframe1.origin.y = nframe1.minY + (nframe1.height - imageSize.height) / 2
  21. nframe1.size.width = imageSize.width
  22. nframe1.size.height = imageSize.height
  23. image.draw(in: nframe1)
  24. // 标题
  25. let cfg = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 12),
  26. NSAttributedString.Key.foregroundColor: UIColor.gray]
  27. let name = title as NSString
  28. let titleSize = name.size(withAttributes: cfg)
  29. nframe2.origin.x = nframe2.minX + (nframe2.width - titleSize.width) / 2
  30. nframe2.origin.y = nframe2.minY + (nframe2.height - titleSize.height) / 2
  31. nframe2.size.width = titleSize.width
  32. nframe2.size.height = titleSize.height
  33. name.draw(in: nframe2, withAttributes: cfg)
  34. }
  35. }