首页 > 移动平台 > 详细

iOS设置textView的placeholder

时间:2018-10-17 19:20:56      阅读:368      评论:0      收藏:0      [点我收藏+]

转载:http://blog.sina.com.cn/s/blog_7a1b23430102wkys.html

 

#import "ViewController.h"

 

@interface ViewController ()<</span>UITextViewDelegate>

{

    UILabel *textViewPlaceholderLabel;

}

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    

    1、在UITextView上加上一个UILabel

    textViewPlaceholderLabel = [[UILabel alloc] initWithFrame:CGRectMake(53, 202, 150, 25)];

    textViewPlaceholderLabel.text = @"请输入你的内容";

    textViewPlaceholderLabel.textColor = [UIColor grayColor];

    

    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(50, 200, 150, 250)];

    textView.delegate = self;

    textView.backgroundColor = [UIColor clearColor];

    textView.layer.borderWidth = 1.0f;

    textView.layer.borderColor = [UIColor blackColor].CGColor;

 

    [self.view addSubview: textViewPlaceholderLabel];

    [self.view addSubview: textView];

    

}

 

//设置textView的placeholder

 

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

{

    //[text isEqualToString:@""] 表示输入的是退格键

    if (![text isEqualToString:@""])

    {

        textViewPlaceholderLabel.hidden = YES;

    }

    

    //range.location == 0 && range.length == 1 表示输入的是第一个字符

    if ([text isEqualToString:@""] && range.location == 0 && range.length == 1)

        

    {

        textViewPlaceholderLabel.hidden = NO;

    }

    return YES;

    

}

iOS设置textView的placeholder

原文:https://www.cnblogs.com/lrr0618/p/9806387.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!