Any suggestions word selected from keyboard suggestion bar always has extra space at the end

时间:2019-01-07 13:53:57

标签: ios uitextfield

Any suggestions word selected from keyboard suggestion bar always has extra space at the end, i want to remove the extra space as soon as text gets filed in to textfield. What is the approach you guys are following for this?

How do you remove and fill text without space to textfield. I wish to avoid the approach of handling it in textFieldShouldReturn because user can see space added to end of text.

This use case is very valid for things like mobile number or postal code.

1 个答案:

答案 0 :(得分:0)

使用此扩展名:

extension String {
    func removeWhitespaces() -> String {
        return components(separatedBy: .whitespaces).joined()
    }
}

然后这样称呼它:

     tf.text = tf.text?.removeWhitespaces()