2011-01-29 9 views

risposta

52

semplice, ho fatto in questo modo .. lavora grande per me .. Spero che questo aiuti qualcuno ..

#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

Ho incluso il codice sopra ma mentre ho modificato la modifica cancella il segnaposto ma in viewdidchange posiziona sempre il segnaposto in modo che l'utente non sia in grado di modificare/digitare in textview c'è qualche soluzione – krishh

+0

Hai @ sugan.s Immagino che tu non stia impostando il colore del testo come '[itsTextView setTextColor: [UIColor lightGrayColor]];' –

+0

Come puoi vedere stiamo cancellando il testo in base al colore del testo .. fammi sapere se è d'aiuto .. –