Created
November 9, 2016 10:54
-
-
Save jaimeagudo/81954347c512a472a32dcc0682ea6e8a to your computer and use it in GitHub Desktop.
Udacity iOS emoji textfield, functionally seasoned
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// EmojiTextFieldDelegate.swift | |
// TextFields | |
// | |
// Created by Jason on 11/11/14. | |
// Copyright (c) 2014 Udacity. All rights reserved. | |
// | |
import Foundation | |
import UIKit | |
// MARK: - EmojiTextFieldDelegate : NSObject, UITextFieldDelegate | |
class EmojiTextFieldDelegate : NSObject, UITextFieldDelegate { | |
// MARK: Properties | |
var translations = [String : String]() | |
// MARK: Initializer | |
override init() { | |
super.init() | |
translations["heart"] = "\u{0001F496}" | |
translations["fish"] = "\u{E522}" | |
translations["bird"] = "\u{E523}" | |
translations["frog"] = "\u{E531}" | |
translations["bear"] = "\u{E527}" | |
translations["dog"] = "\u{E052}" | |
translations["cat"] = "\u{E04F}" | |
} | |
// MARK: When TextField Changes | |
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { | |
let replacedText = (textField.text! as NSString).stringByReplacingCharactersInRange(range, withString: string); | |
// For each dictionary entry in translations, pull out a string to search for and an emoji to replace it with | |
let translatedText=translations.reduce(replacedText as String) { | |
$0.stringByReplacingOccurrencesOfString($1.0, withString: $1.1, options: NSStringCompareOptions.CaseInsensitiveSearch) | |
} | |
textField.text = translatedText; | |
return false; | |
} | |
func textFieldDidBeginEditing(textField: UITextField) { | |
textField.text = "" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment