[英]How can I move the cursor to the beginning of a UITextField in Swift?
我已經多次問過這個問題,但是每個答案似乎都是用目標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的開始,然后將選定的兩個都設置為開始。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.