EmojisCollectionViewDelegate.swift 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //
  2. // EmojisCollectionViewDelegate.swift
  3. // Photo Editor
  4. //
  5. // Created by Mohamed Hamed on 4/30/17.
  6. // Copyright © 2017 Mohamed Hamed. All rights reserved.
  7. //
  8. import UIKit
  9. class EmojisCollectionViewDelegate: NSObject, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
  10. var stickersViewControllerDelegate : StickersViewControllerDelegate?
  11. let emojiRanges = [
  12. 0x1F601...0x1F64F, // emoticons
  13. 0x1F30D...0x1F567, // Other additional symbols
  14. 0x1F680...0x1F6C0, // Transport and map symbols
  15. 0x1F681...0x1F6C5 //Additional transport and map symbols
  16. ]
  17. var emojis: [String] = []
  18. override init() {
  19. super.init()
  20. for range in emojiRanges {
  21. for i in range {
  22. let c = String(describing: UnicodeScalar(i)!)
  23. emojis.append(c)
  24. }
  25. }
  26. }
  27. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  28. return emojis.count
  29. }
  30. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  31. let emojiLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 90, height: 90))
  32. emojiLabel.textAlignment = .center
  33. emojiLabel.text = emojis[indexPath.item]
  34. emojiLabel.font = UIFont.systemFont(ofSize: 70)
  35. stickersViewControllerDelegate?.didSelectView(view: emojiLabel)
  36. }
  37. func numberOfSections(in collectionView: UICollectionView) -> Int {
  38. return 1
  39. }
  40. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  41. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "EmojiCollectionViewCell", for: indexPath) as! EmojiCollectionViewCell
  42. cell.emojiLabel.text = emojis[indexPath.item]
  43. return cell
  44. }
  45. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  46. return 4
  47. }
  48. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  49. return 0
  50. }
  51. }