zoukankan      html  css  js  c++  java
  • iOS设置textView的placeholder

    转载: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;

        

    }

  • 相关阅读:
    欧几里德算法
    int 和 string 相互转换(简洁版)
    骆驼吃香蕉
    链表反转 (Multi-method)
    二分查找 (最经典代码,及其边界条件的实践分析)
    mottoes
    欧拉函数,欧拉定理,费马小定理。
    深搜和广搜的对比
    Python基础
    马拉车求最大回文字串
  • 原文地址:https://www.cnblogs.com/lrr0618/p/9806387.html
Copyright © 2011-2022 走看看