Reputation: 45
i am using textview in iOS application but i am facing some problem when i am set placeholder in my textview the text not hide when click in textview so can any here who work placeholder in textview
Upvotes: 0
Views: 3294
Reputation: 897
It is not possible to create placeholder in UITextView but you can generate effect like place holder by this.
- (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];
}
}
OR you can add label in textview just like
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];
and set
- (void)textViewDidEndEditing:(UITextView *) textView
{
if (![textView hasText]) {
lbl.hidden = NO;
}
}
- (void) textViewDidChange:(UITextView *)textView
{
if(![textView hasText]) {
lbl.hidden = NO;
}
else{
lbl.hidden = YES;
}
}
Upvotes: 1
Reputation: 11
Try This
1.Create a label and show or hide
-(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];
}
Upvotes: 1
Reputation: 5108
NOTE: There isn't any inbuilt function for placeholder in textviews like textfields.
create an IBOutlet
to your textView
in .h
file like below
and add <UITextViewDelegate>
like below
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UITextView *mytextView;
@end
then in your .m
file, implement textview
delegate
methods like below.
and if you want some addition, try following method also with above code
- (void)textViewDidChange:(UITextView *)textView
{
NSString *mtvt = textView.text;
if ([mtvt isEqualToString:@""]) {
self.mytextView.text = @"Your message here";
}
}
Upvotes: 3
Reputation: 828
You can use textView delegate methods to manage placeholder text programatically
- (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];
}
Upvotes: 5