프로그래밍 방식으로 UITextField의 모든 텍스트 선택
UITextField의 모든 텍스트를 프로그래밍 방식으로 어떻게 선택할 수 있습니까?
결과적으로 -selectAll :을 호출하면 nil이 아닌 보낸 사람이 메뉴를 표시합니다. nil로 호출하면 텍스트가 선택되지만 메뉴는 표시되지 않습니다.
나는 버그 보고서가 Apple에서 자신 대신에 nil을 통과하라는 제안으로 돌아온 후에 이것을 시도했습니다.
UIMenuController 또는 다른 선택 API를 사용할 필요가 없습니다.
그것이 나를 위해 트릭을 한 것입니다.
[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에게 감사합니다 (여기에있는 주석에서 방금 편집).
위의 Mirko의 주석을 확인하기 위해 이것을 테스트했지만 selectAll:실제로 UITextField 자체로 보낼 때 모든 텍스트를 선택 하는지 확인 합니다.
텍스트는 CUT | 복사 | PASTE 작업이지만 질문에 대해서는 사용자가 시작하기 위해 "모두 선택"을 탭할 때 나타나는 것과 정확히 같습니다.
내가 갈 해결책은 다음과 같습니다. 두 번째 줄은 명시적인 사용자 선택을 위해 비활성화하지 않고 CUT / COPY / PASTE 대화 상자를 일시적으로 숨 깁니다.
[_myTextField selectAll:self];
[UIMenuController sharedMenuController].menuVisible = NO;
필요한 것을 사용하십시오
ObjC
[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)
빠른
의 모든 텍스트 선택 UITextField:
textField.selectedTextRange = textField.textRangeFromPosition(textField.beginningOfDocument, toPosition: textField.endOfDocument)
내 전체 답변은 여기에 있습니다 .
이것이 내가 찾은 최고의 솔루션입니다. sharedMenuController가 없으며 연속적으로 작동합니다.
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[textField performSelector:@selector(selectAll:) withObject:nil afterDelay:0.1];
}
텍스트를 선택하려면 텍스트 필드를 편집 할 수 있어야합니다. 텍스트 필드를 편집 할 수있는시기를 확인하려면 대리자 메서드를 사용하십시오.
- (void)textFieldDidBeginEditing:(UITextField *)textField
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
textFieldShouldBeginEditing :이 필요하다고 생각하지 않지만 구현에서 사용한 것입니다.
- (void)textFieldDidBeginEditing:(UITextField *)textField{
[textField selectAll:textField];
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
return YES;
}
selectAll에 nil을 전달하면 메뉴가 표시되지 않습니다.
Unfortunately I don't think you can do that.
I'm not sure if this helps you, but setClearsOnBeginEditing lets you specify that the UITextField should delete the existing value when the user starts editing (this is the default for secure UITextFields).
Swift 3:
textField.selectAll(self)
I create a custom alert view which contains a UITextField inside. I found a problem to the textfield is that: beginningOfDocument only has value if textfield is added to screen & becomeFirstResponder is called.
Otherwise beginningOfDocument returns nil and [UITextField textRangeFromPosition:] can not get the value.
So here is my sample code to solve this case.
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)];
Done!
If you mean how would you allow the user to edit the text in a uitextfield then just assign firstResponder to it:
[textField becomeFirstResponder]
If you mean how do you get the text in the uitextfield than this will do it:
textField.text
If you mean actually select the text (as in highlight it) then this will may be useful:
참고URL : https://stackoverflow.com/questions/1689911/programmatically-select-all-text-in-uitextfield
'IT TIP' 카테고리의 다른 글
| Android에서 URL이 유효한지 확인하는 방법 (0) | 2020.11.02 |
|---|---|
| Keras의 HDF5 파일에서 모델을로드하는 방법은 무엇입니까? (0) | 2020.11.02 |
| 버튼 클릭으로 테이블 행의 내용 가져 오기 (0) | 2020.11.02 |
| Android Studio에서 AVD 에뮬레이터 창의 크기를 조정하는 방법은 무엇입니까? (0) | 2020.11.02 |
| 시작할 때 시작할 프로그램을 어떻게 설정합니까? (0) | 2020.11.02 |