gpt4 book ai didi

ios - 如何将光标移动到 Swift 中 UITextField 的开头?

转载 作者:行者123 更新时间:2023-11-29 01:17:02 26 4
gpt4 key购买 nike

这个问题我已经看过很多次了,但是每一个答案似乎都是用objective c写的,我不知道也不知道如何转换成Swift。

我有一个文本字段,我希望用户在其中输入百分比。我有它,所以当他们开始编辑文本框时,占位符文本消失并被百分号取代。

我希望这个百分号始终保留在输入的末尾。我似乎无法弄清楚如何将光标移回文本框的开头来实现这一点。

这是我开始编辑操作的代码(这包括另一个文本框,用户在其中输入美元金额,但美元符号排在第一位,所以这没什么大不了的)

    @IBAction func textBoxBeginEditing(sender: UITextField) {
// Dismiss keyboard if the main view is tapped
tapRecognizer.addTarget(self, action: "didTapView")
view.addGestureRecognizer(tapRecognizer)

// If there's placeholder text, remove it and change text color to black
if (sender.textColor == UIColor.lightGrayColor()) {
sender.text = nil
sender.textColor = UIColor.blackColor()
}

// Force the keyboard to be a number pad
sender.keyboardType = UIKeyboardType.NumberPad

// Set up symbols in text boxes
if (sender == deductibleTextBox) {
sender.text = "$"
}
if (sender == percentageTextBox) {
sender.text = "%"

// This part doesn't do anything... Need a solution
let desiredPosition = sender.beginningOfDocument
sender.selectedTextRange = sender.textRangeFromPosition(desiredPosition, toPosition: desiredPosition)

}
}

最后一点是我从互联网上寻求帮助的全部内容。我正在创建的这个应用程序完全是 iOS 学习曲线,所以如果这是一个愚蠢的问题,我深表歉意。

最佳答案

let newPosition = textView.beginningOfDocument

textView.selectedTextRange = textView.textRangeFromPosition(newPosition, toPosition: newPosition)

在此,我们获取 TextView 的开头,然后将选定的两者设置为开头。

关于ios - 如何将光标移动到 Swift 中 UITextField 的开头?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35052431/

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