My Blog List

Android FirstAid Coding

A Small Help From a Small Heart
Powered by Blogger.

A software professional, who still likes to code, likes to blog and loves gadgets.

Sunday 28 July 2013

How to show placeholder in UITextView iPhone

ViewController.h
------------------

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITextViewDelegate>{
    UITextView *commentTf;
}

@end

ViewController.m
-----------------

#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
    [super viewDidLoad];   
    commentTf  = [[UITextView alloc] initWithFrame:CGRectMake(7, 40, 306, 85)];   
    [commentTf setFont:[UIFont systemFontOfSize:17.0]];   
    [commentTf setTextColor:[UIColor lightGrayColor]];      
    [commentTf setAutocapitalizationType:UITextAutocapitalizationTypeSentences];     
    commentTf.delegate=self;
    commentTf.text =@"Write your comment here";   
    [self.view addSubview:commentTf];

}
- (BOOL) textViewShouldBeginEditing:(UITextView *)textView
{
    if (commentTf.textColor == [UIColor lightGrayColor]) {
        commentTf.text = @"";
        commentTf.textColor = [UIColor blackColor];
    }   
    return YES;
}
-(void) textViewDidChange:(UITextView *)textView
{
    if(commentTf.text.length == 0){
        commentTf.textColor = [UIColor lightGrayColor];
        commentTf.text = @"Write your comment here";
        [commentTf resignFirstResponder];
    }
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {   
    if([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        if(commentTf.text.length == 0){
            commentTf.textColor = [UIColor lightGrayColor];
            commentTf.text = @"Write your comment here";
            [commentTf resignFirstResponder];
        }
        return NO;
    }   
    return YES;
}
@end


1 comment:

  1. think your blog is pretty good which have some useful information. Thanks for sharing us!
    Your contents are too simple to read and easy to understand.
    -------------------------------------------------------------------------------
    iPhone App Development :: && ::Mobile and Web Application Development

    ReplyDelete