接近完美禁用iOS的emoji表情键盘

支持iOS搜狗输入法和自动关联表情(如输入hehe,出来的表情)

注意:Swift的textView.text != disable_emoji(textView.text)

在OC里面用 ![xxx isEqualString:bbb]

转帖保留(谢谢):http://www.cnblogs.com/Leetvin/p/4384141.html 

@IBOutlet weak var tf: UITextField!

    override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.

        

        tf.addTarget(self, action: "textFieldDidChange:", forControlEvents: UIControlEvents.EditingChanged)

    }

 

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

 

    func textViewDidChange(textView: UITextView) {

        var textTange = textView.selectedTextRange

        if(textView.text != disable_emoji(textView.text))

        {

            textView.text = disable_emoji(textView.text)

        }

        textView.selectedTextRange = textTange

    }

    

    func textFieldDidChange(textField: UITextField) {

        

        var textTange = textField.selectedTextRange

        if(textField.text != disable_emoji(textField.text))

        {

            textField.text = disable_emoji(textField.text)

        }

        textField.selectedTextRange = textTange

    }

    

    func disable_emoji(text:String)->String

    {

        var regex = NSRegularExpression(pattern: "[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]", options: NSRegularExpressionOptions.CaseInsensitive, error: nil)

        var modifiedString = regex!.stringByReplacingMatchesInString(text, options: NSMatchingOptions(), range: NSMakeRange(0, text.utf16Count), withTemplate: "")

        return modifiedString

    }

posted @ 2015-04-01 15:57  LeeTVin  阅读(2593)  评论(0编辑  收藏  举报