thể trùng lặp:
Placeholder in UITextViewCách thêm placeHolder Text trong UItextView? trong iphone sdk
Trong iPhone App Làm thế nào để thêm văn bản giữ chỗ (để giữ một số văn bản mặc định) trong UItextView
?
thể trùng lặp:
Placeholder in UITextViewCách thêm placeHolder Text trong UItextView? trong iphone sdk
Trong iPhone App Làm thế nào để thêm văn bản giữ chỗ (để giữ một số văn bản mặc định) trong UItextView
?
đơn giản, tôi đã làm nó theo cách này .. làm việc tuyệt vời cho tôi .. Hy vọng điều này sẽ giúp một số một ..
#pragma mark -
#pragma mark TextView Delegate methods
UITextView itsTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, itsTextView.frame.size.width, itsTextView.frame.size.height)];
[itsTextView setDelegate:self];
[itsTextView setReturnKeyType:UIReturnKeyDone];
[itsTextView setText:@"List words or terms separated by commas"];
[itsTextView setFont:[UIFont fontWithName:@"HelveticaNeue" size:11]];
[itsTextView setTextColor:[UIColor lightGrayColor]];
- (BOOL) textViewShouldBeginEditing:(UITextView *)textView
{
if (itsTextView.textColor == [UIColor lightGrayColor]) {
itsTextView.text = @"";
itsTextView.textColor = [UIColor blackColor];
}
return YES;
}
-(void) textViewDidChange:(UITextView *)textView
{
if(itsTextView.text.length == 0){
itsTextView.textColor = [UIColor lightGrayColor];
itsTextView.text = @"List words or terms separated by commas";
[itsTextView resignFirstResponder];
}
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
if(itsTextView.text.length == 0){
itsTextView.textColor = [UIColor lightGrayColor];
itsTextView.text = @"List words or terms separated by commas";
[itsTextView resignFirstResponder];
}
return NO;
}
return YES;
}
Tôi đã bao gồm mã trên nhưng trong khi chỉnh sửa didbegin nó sẽ xóa trình giữ chỗ nhưng trong viewdidchange, nó luôn đặt trình giữ chỗ để người dùng không thể chỉnh sửa/nhập trong chế độ xem văn bản là có giải pháp – krishh
Hai @ sugan.s Tôi đoán bạn không thiết lập màu văn bản là '[itsTextView setTextColor: [UIColor lightGrayColor]];' –
Như bạn có thể thấy chúng tôi đang xóa văn bản dựa trên màu văn bản .. hãy cho tôi biết nếu nó giúp .. –
Hãy chỉnh sửa câu hỏi của bạn với một lời giải thích về những gì bạn có nghĩa là bằng cách "giữ chỗ" –
chí này giúp bạn: http://stackoverflow.com/a/17451491/1442541 – evya