gpt4 book ai didi

iphone - 如何在UITextView中插入占位符?

转载 作者:行者123 更新时间:2023-12-03 18:10:49 25 4
gpt4 key购买 nike

有没有办法在 UITextView 中插入占位符类似于UITextField ?如果是,请向我发送任何链接或任何实现此功能的想法。

最佳答案

无法在 UITextView 中创建占位符,但可以通过它生成类似占位符的效果。

- (void)viewDidLoad {   
commentTxtView.text = @"Comment";
commentTxtView.textColor = [UIColor lightGrayColor];
commentTxtView.delegate = self;

}
- (BOOL) textViewShouldBeginEditing:(UITextView *)textView {
commentTxtView.text = @"";
commentTxtView.textColor = [UIColor blackColor];
return YES;
}

-(void) textViewDidChange:(UITextView *)textView {

if(commentTxtView.text.length == 0) {
commentTxtView.textColor = [UIColor lightGrayColor];
commentTxtView.text = @"Comment";
[commentTxtView resignFirstResponder];
}
}

-(void) textViewShouldEndEditing:(UITextView *)textView {

if(commentTxtView.text.length == 0) {
commentTxtView.textColor = [UIColor lightGrayColor];
commentTxtView.text = @"Comment";
[commentTxtView resignFirstResponder];
}
}

或者您可以在 TextView 中添加标签,就像

lbl = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 0.0,textView.frame.size.width - 10.0, 34.0)];

[lbl setText:kDescriptionPlaceholder];
[lbl setBackgroundColor:[UIColor clearColor]];
[lbl setTextColor:[UIColor lightGrayColor]];
textView.delegate = self;

[textView addSubview:lbl];

并设置

- (void)textViewDidEndEditing:(UITextView *) textView {
if (![textView hasText]) {
lbl.hidden = NO;
}
}

- (void) textViewDidChange:(UITextView *)textView {
if(![textView hasText]) {
lbl.hidden = NO;
}
else {
lbl.hidden = YES;
}
}

关于iphone - 如何在UITextView中插入占位符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7038876/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com