2011-01-29 28 views

Trả lời

52

đơ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; 
} 
+0

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

+0

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]];' –

+0

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 .. –

Các vấn đề liên quan