簡體   English   中英

以編程方式選擇 UITextField 中的所有文本

[英]Programmatically Select all text in UITextField

如何以編程方式選擇 UITextField 中的所有文本?

這就是我的訣竅:

[self.titleField setSelectedTextRange:[self.titleField textRangeFromPosition:self.titleField.beginningOfDocument toPosition:self.titleField.endOfDocument]];

相當丑陋但它有效,所以不會顯示 sharedMenuController !

要解決“每隔一次才有效”的問題,請使用以下命令:

__weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
    __strong __typeof(weakSelf) strongSelf = weakSelf;
    UITextRange *range = [strongSelf textRangeFromPosition:strongSelf.beginningOfDocument toPosition:strongSelf.endOfDocument];
    [strongSelf setSelectedTextRange:range];
});

感謝 Eric Baker(剛剛從這里的評論中編輯)

事實證明,使用非零發送者調用 -selectAll: 會顯示菜單。 用 nil 調用它會導致它選擇文本,但不顯示菜單。

在我的錯誤報告從 Apple 返回並建議我通過 nil 而不是 self 之后,我嘗試了這個。

無需使用 UIMenuController 或其他選擇 API。

我只是對此進行了測試以驗證上面 Mirko 的評論,但是我的測試驗證了selectAll:實際上在將文本發送到 UITextField 本身時確實選擇了所有文本。

請注意,文本將立即被 CUT | 遮擋​​。 復制 | PASTE 操作,但對於您的問題,這正是用戶點擊“全選”開始時出現的內容。

我要使用的解決方案如下,請注意,第二行將暫時隱藏 CUT/COPY/PASTE 對話框,而不會為明確的用戶選擇禁用它

[_myTextField selectAll:self];
[UIMenuController sharedMenuController].menuVisible = NO;

使用你需要的

對象

[yourtextField becomeFirstResponder]; //puts cursor on text field
[yourtextField selectAll:nil];  //highlights text
[yourtextField selectAll:self]; //highlights text and shows menu(cut copy paste)

迅速

yourTextField.becomeFirstResponder() //puts cursor on text field
yourTextField.selectAll(nil)  //highlights text
yourTextField.selectAll(self) //highlights text and shows menu(cut copy paste)

這是我找到的最好的解決方案。 沒有 sharedMenuController,它連續工作:

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    [textField performSelector:@selector(selectAll:) withObject:nil afterDelay:0.1];
}

迅速

選擇UITextField所有文本:

textField.selectedTextRange = textField.textRange(from: textField.beginningOfDocument, to: textField.endOfDocument)

我的完整答案在這里: https : //stackoverflow.com/a/34922332/3681880

為了能夠選擇文本,文本字段必須是可編輯的。 要知道文本字段何時可編輯,請使用委托方法:

- (void)textFieldDidBeginEditing:(UITextField *)textField
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField

我不認為 textFieldShouldBeginEditing: 是必需的,但這是我在實現中使用的。

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    [textField selectAll:textField];
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    return YES;
}

將 nil 傳遞給 selectAll:不會顯示菜單。

不幸的是,我認為你不能這樣做。

我不確定這是否對您有幫助,但是setClearsOnBeginEditing允許您指定UITextField應在用戶開始編輯時刪除現有值(這是安全UITextFields的默認值)。

斯威夫特 3:

textField.selectAll(self)

我創建了一個自定義警報視圖,其中包含一個UITextField 我發現文本字段的一個問題是: beginningOfDocument如果文本字段添加到屏幕和僅有值becomeFirstResponder被調用。

否則beginningOfDocument回報nil[UITextField textRangeFromPosition:]無法獲得的價值。

所以這是我解決這種情況的示例代碼。

UIWindow *window = [[[UIApplication sharedApplication] windows] firstObject];
[window addSubview:theAlertView]; // textfield must be added as a subview of screen first
UITextField *textField = theAlertView.textField;
[textField becomeFirstResponder]; // then call to show keyboard and cursor 
UITextRange *range = [textField textRangeFromPosition:textField.beginningOfDocument toPosition:textField.endOfDocument]; // at this time, we could get beginningOfDocument
[textField setSelectedTextRange:range]; // Finally, it works!!!
UITextField *tf = yourTF;
// hide cursor (you have store default color!!!)
[[tf valueForKey:@"textInputTraits"] setValue:[UIColor clearColor]
                                       forKey:@"insertionPointColor"];
// enable selection
[tf selectAll:self];
// insert your string here
// and select nothing (!!!)
[tf setMarkedText:@"Egor"
    selectedRange:NSMakeRange(0, 0)];

完畢!

如果您的意思是如何允許用戶編輯 uitextfield 中的文本,那么只需將 firstResponder 分配給它:

[textField becomeFirstResponder]

如果您的意思是如何獲取 uitextfield 中的文本,則可以這樣做:

textField.text

如果您的意思是實際選擇文本(如突出顯示),那么這可能很有用:

全選

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM