如何在iOS中为自定义键盘实现自定义的退格/删除按钮,以快速响应UITextField委托?
我为此挣扎了一段时间,以为我会回答这个问题。以下代码响应在作为UITextfield的第一响应者的uikeyboard上按下的自定义退格键:
// the tag for the backspace button
if tag == "<"{
//the range of the selected text (handles multiple highlighted characters of a uitextfield)
let range = textField.selectedTextRange?;
// if the range is empty indicating the cursor position is
// singular (no multiple characters are highlighted) then delete the character
// in the position before the cursor
if range!.empty {
let start = textField.positionFromPosition(range!.start, offset: -1);
let end = axiomBuilderTV.positionFromPosition(range!.end, offset: 0);
axiomBuilderTV.replaceRange(axiomBuilderTV.textRangeFromPosition(start, toPosition: end), withText: "");
}
// multiple characters have been highlighted so remove them all
else {
textField.replaceRange(range!, withText: "");
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句