gpt4 book ai didi

swift - 如何在自定义键盘按钮 swift 中实现功能齐全的退格键

转载 作者:行者123 更新时间:2023-11-28 13:17:54 26 4
gpt4 key购买 nike

如何在 iOS 中为自定义键盘实现自定义退格/删除按钮,以快速响应 UITextField 委托(delegate)?

最佳答案

我为此苦苦挣扎了一段时间,所以我想我会回答这个问题。以下代码响应在作为 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: "");
}

}

关于swift - 如何在自定义键盘按钮 swift 中实现功能齐全的退格键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28212545/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com