题 如何在UItextView中添加placeHolder文本?在iPhone sdk [重复]


可能重复:
占位符在UITextView中 

在iPhone App中如何添加placeHolder文本(以保存一些默认文本) UItextView


16
2018-01-29 06:48


起源


请编辑您的问题,并解释“占位符”的含义 - Dave DeLong
这将有助于您: stackoverflow.com/a/17451491/1442541 - evya


答案:


很简单,我这样做..为我工作很好..希望这有助于一些..

#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;
}

52
2018-06-12 12:59



我包含上面的代码但是在编辑它时它会清除占位符但是在viewdidchange中它总是放置占位符,因此用户无法编辑/输入textview是否有任何解决方案 - Sugan S
Hai @ sugan.s我猜你没有将文本颜色设置为'[itsTextView setTextColor:[UIColor lightGrayColor]];' - Dilip Rajkumar
正如您所看到的,我们正在根据文本颜色清除文本..让我知道它是否有帮助.. - Dilip Rajkumar
我会在这里包含代码吗? - Sugan S
请给我发送代码到dilip.id@me.com恶意尝试帮助,或者您可以将其作为新问题发布并在此处给出链接.. - Dilip Rajkumar