ios objective c 中的 TextView 占位符
TextView Placeholder in ios objective c
我在 iOS 应用程序中使用 textview,但是当我在我的 textview 中设置占位符时,我遇到了一些问题,当我在 textview 中单击时,文本不会隐藏,所以任何在 textview 中使用占位符的人都可以
您可以使用 textView 委托方法以编程方式管理占位符文本
- (void)textViewDidBeginEditing:(UITextView *)textView
{
if ([textView.text isEqualToString:@"myPlaceHolderText"])
{
textView.text = @"";
}
[textView becomeFirstResponder];
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
if ([textView.text isEqualToString:@""]) {
textView.text = @"myPlaceHolderText";
}
[textView resignFirstResponder];
}
注意:在像文本字段这样的文本视图中,没有任何用于占位符的内置函数。
在 .h
文件中为您的 textView
创建一个 IBOutlet
,如下所示
并像下面那样添加 <UITextViewDelegate>
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UITextView *mytextView;
@end
然后在您的 .m
文件中,实现 textview
delegate
方法,如下所示。
如果你想要一些补充,也可以尝试使用上面的代码
- (void)textViewDidChange:(UITextView *)textView
{
NSString *mtvt = textView.text;
if ([mtvt isEqualToString:@""]) {
self.mytextView.text = @"Your message here";
}
}
Try This
1.Create 一个标签并显示或隐藏
-(void)viewdidLoad
{
UILabel*placeHolderLabel = [UILabel alloc]initWithFrame:CGRectMake(0,0,300,30)];
placeHolderLabel.text = @"PlaceHolderText";
placeHolderLabel.textColour = [UIColour lightGrayColor];
[yourTextView addSubView:placeHolderLabel];
}
//2.TextViewDelegate
- (void)textViewDidBeginEditing:(UITextView *)textView
{
placeHolderLabel.hidden = YES;
[textView becomeFirstResponder];
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
if ([textView.text isEqualToString:@""]) {
placeHolderLabel.hidden = NO;
}
[textView resignFirstResponder];
}
无法在 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];
}
}
或者您可以在文本视图中添加标签,就像
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;
}
}
我在 iOS 应用程序中使用 textview,但是当我在我的 textview 中设置占位符时,我遇到了一些问题,当我在 textview 中单击时,文本不会隐藏,所以任何在 textview 中使用占位符的人都可以
您可以使用 textView 委托方法以编程方式管理占位符文本
- (void)textViewDidBeginEditing:(UITextView *)textView
{
if ([textView.text isEqualToString:@"myPlaceHolderText"])
{
textView.text = @"";
}
[textView becomeFirstResponder];
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
if ([textView.text isEqualToString:@""]) {
textView.text = @"myPlaceHolderText";
}
[textView resignFirstResponder];
}
注意:在像文本字段这样的文本视图中,没有任何用于占位符的内置函数。
在 .h
文件中为您的 textView
创建一个 IBOutlet
,如下所示
并像下面那样添加 <UITextViewDelegate>
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UITextView *mytextView;
@end
然后在您的 .m
文件中,实现 textview
delegate
方法,如下所示。
如果你想要一些补充,也可以尝试使用上面的代码
- (void)textViewDidChange:(UITextView *)textView
{
NSString *mtvt = textView.text;
if ([mtvt isEqualToString:@""]) {
self.mytextView.text = @"Your message here";
}
}
Try This
1.Create 一个标签并显示或隐藏
-(void)viewdidLoad
{
UILabel*placeHolderLabel = [UILabel alloc]initWithFrame:CGRectMake(0,0,300,30)];
placeHolderLabel.text = @"PlaceHolderText";
placeHolderLabel.textColour = [UIColour lightGrayColor];
[yourTextView addSubView:placeHolderLabel];
}
//2.TextViewDelegate
- (void)textViewDidBeginEditing:(UITextView *)textView
{
placeHolderLabel.hidden = YES;
[textView becomeFirstResponder];
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
if ([textView.text isEqualToString:@""]) {
placeHolderLabel.hidden = NO;
}
[textView resignFirstResponder];
}
无法在 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];
}
}
或者您可以在文本视图中添加标签,就像
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;
}
}