Emojione.swift 1.0 KB

123456789101112131415161718192021222324252627282930313233
  1. //
  2. // Emojione.swift
  3. //
  4. // Created by Rafael Kellermann Streit (@rafaelks) on 10/10/16.
  5. // Copyright (c) 2016.
  6. //
  7. import Foundation
  8. struct Emojione {
  9. static let values = [
  10. <%= mapping %>
  11. ]
  12. static func transform(string: String) -> String {
  13. let oldString = string as NSString
  14. var transformedString = string as NSString
  15. let regex = try! NSRegularExpression(pattern: ":([-+\\w]+):", options: [])
  16. let matches = regex.matches(in: transformedString as String, options: [], range: NSMakeRange(0, transformedString.length))
  17. for result in matches {
  18. guard result.numberOfRanges == 2 else { continue }
  19. let shortname = oldString.substring(with: result.rangeAt(1))
  20. if let emoji = values[shortname] {
  21. transformedString = transformedString.replacingOccurrences(of: ":\(shortname):", with: emoji) as NSString
  22. }
  23. }
  24. return transformedString as String
  25. }
  26. }