2011-01-29 12 views

Odpowiedz

52

Proste, zrobiłem to w ten sposób .. działa doskonale dla mnie .. Nadzieja to pomaga kogoś ..

#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

Zawarłem powyższy kod, ale podczas edytowania didbegin usuwa znak zastępczy, ale w viewdidchange zawsze umieszcza symbol zastępczy, aby użytkownik nie mógł edytować/wpisać w tekście jest dowolny rozwiązanie – krishh

+0

Hai @ sugan.s Domyślam się, że nie ustawiasz koloru tekstu jako '[itsTextView setTextColor: [UIColor lightGrayColor]];' –

+0

Jak widać usuwamy tekst w oparciu o kolor tekstu .. daj mi znać, jeśli to pomoże .. –