码迷,mamicode.com
首页 > 其他好文 > 详细

UITextView加占位符placeholder

时间:2015-07-02 15:34:35      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:

@property (nonatomic ,retain) UITextView * contentText;   

@property (nonatomic ,retain) NSString * placeholderText; 

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

 

    _placeholderText=@"这一刻的想法...";

    _contentText = [[UITextView alloc] initWithFrame:CGRectMake(10, 5,260, 120)];
    _contentText.backgroundColor = [UIColor clearColor];
    _contentText.text = _placeholderText;
    _contentText.textColor=[UIColor grayColor];
    _contentText.font = [UIFont systemFontOfSize:16];
    _contentText.returnKeyType = UIReturnKeyDone;
    _contentText.delegate = self;
    [self.view addSubview:_contentText];

}

//加占位符
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    
    if ([textView.text isEqualToString:_placeholderText]) {
        textView.text=@"";
    }
    textView.textColor=[UIColor blackColor];
    
    return YES;
}
- (void)textViewDidEndEditing:(UITextView *)textView{
    
    if ([textView.text isEqualToString:@""]) {
        
        textView.text=_placeholderText;
        textView.textColor=[UIColor grayColor];
    }
    
}

 

//点击回车放弃键盘

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    
    if ([text isEqualToString:@"\n"]) {
        
        [textView resignFirstResponder];
        
        return NO;
        
    }
    
    return YES;
    
}

UITextView加占位符placeholder

标签:

原文地址:http://www.cnblogs.com/huangzs/p/4615985.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!