UITextView实现placeHolder方法汇总

UITextField中有一个placeholder属性,可以设置UITextField的占位文字,起到提示用户的作用。可是UITextView就没那么幸运了,apple没有给UITextView提供一个类似于placeholder这样的属性来供开发者使用。下面我就把自己能够想到的和网友提供的方法汇总一下,让更多的开发者知道,原来有这么多方法可以实现UITextView的占位文字。

方法一:

1.把UITextView的text属性当成“placeholder”使用。

2.在开始编辑的代理方法里清除“placeholder”。

3.在结束编辑的代理方法里根据条件设置“placeholder”。

特点:这种方法的特点是,当用户点击了textView,placeholder占位文字就会立马消失,官方的placeholder是当系统监听到用户输入了文字后placeholder才会消失。

// 创建textView
UITextView *textView =[[UITextViewalloc]initWithFrame:CGRectMake(20,70,SCREEN.width-40,100)];
textView.backgroundColor= [UIColor whiteColor];
textView.text = @"我是placeholder";
textView.textColor = [UIColor grayColor];
textView.delegate = self;
[self.view addSubview:textView];

#pragma mark - UITextViewDelegate
- (void)textViewDidEndEditing:(UITextView *)textView
{
    if(textView.text.length < 1){
        textView.text = @"我是placeholder";
        textView.textColor = [UIColor grayColor];
    }
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    if([textView.text isEqualToString:@"我是placeholder"]){
        textView.text=@"";
        textView.textColor=[UIColor blackColor];
    }
}

方法二:

1.创建textView

2.给textView添加一个UILabel子控件,作为placeholder

3.在文本改变的代理方法里面显示/隐藏UILabel

特点:该方法同样也可以实现类似于placeholder的功能。相比较方法一,方法二可以实现动态监听文本的改变,并非弹出键盘就立即清除placeholder,只有当用户开始输入文本的时候。placeholder才会消失。同样,当用户清空文本的时候,placeholder又会重新显示出来。

同样地思路,我们也可以用两个UITextView来实现placeholder的功能,再次就不在详述。

#import "WSViewController.h"

@interface WSViewController () <UITextViewDelegate>

@property(nonatomic, weak)UITextView *textView;

@property(nonatomic, weak)UILabel *placeHolder;

@end

@implementation WSViewController

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

    [self setupTextView];

}

// 添加textView
- (void)setupTextView
{
    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 74, SCREEN_WIDTH - 2 * 10, 200)];
    textView.frame = CGRectMake(10, 74, SCREEN_WIDTH - 2 * 10, 200);

    [self.view addSubview:textView];
    self.textView = textView;

    textView.contentInset = UIEdgeInsetsMake(-64, 0, 0, 0);

    textView.delegate = self;
    [self setupPlaceHolder];

    //在弹出的键盘上面加一个view来放置退出键盘的Done按钮
    UIToolbar * topView = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
    [topView setBarStyle:UIBarStyleDefault];
    UIBarButtonItem * btnSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    UIBarButtonItem * doneButton = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(dismissKeyBoard)];
    NSArray * buttonsArray = [NSArray arrayWithObjects:btnSpace, doneButton, nil];

    [topView setItems:buttonsArray];
    [textView setInputAccessoryView:topView];

}

// 给textView添加一个UILabel子控件
- (void)setupPlaceHolder
{
    UILabel *placeHolder = [[UILabel alloc] initWithFrame:CGRectMake(15, -2, SCREEN_WIDTH - 2 * 15, 200)];
    self.placeHolder = placeHolder;

    placeHolder.text = @"我是placeholder";
    placeHolder.textColor = [UIColor lightGrayColor];
    placeHolder.numberOfLines = 0;
    placeHolder.contentMode = UIViewContentModeTop;
    [self.view addSubview:placeHolder];
}

#pragma mark - UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView
{
    if (!textView.text.length) {
    self.placeHolder.alpha = 1;
    } else {
        self.placeHolder.alpha = 0;
    }
}

//关闭键盘
-(void) dismissKeyBoard{
    [self.textView resignFirstResponder];
}

@end

方法三:

1.添加textView

2.给textView的layer绘制文字

3.在文本改变的方法里控制绘制文字的显示/隐藏

方法四:

1.利用runtime便利出来UITextView的placeholder属性,然后给该属性赋值。

时间: 2024-08-09 10:39:49

UITextView实现placeHolder方法汇总的相关文章

UITextView 实现placeholder的方法

本文转载至 http://www.cnblogs.com/easonoutlook/archive/2012/12/28/2837665.html 在UITextField中自带placeholder属性,可以用于提示输入框信息.但是UITextView并不具备此功能 介绍两种方法来实现: 第一种: 初始化UITextView //首先定义UITextView UITextView *textView = [[UITextView alloc] init]; textView.font = [U

IOS UITextView加上placeholder

UITextView上如何加上类似于UITextField的placeholder呢,其实在UITextView上加上一个UILabel,然后再实现 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text这个代理方法就可以了. 具体实现如下: - (BOOL)textView:(UITextView *)textView shou

自定义UITextView实现placeholder效果

今天项目中有一个界面需要通过UITextView实现,并且具有UITextField的placeholder功能,自己昨晚之后觉得蛮有趣的,拿出来分享一下. 先放最终效果完成图: 具体思路就是创建一个UITextView,然后在其上面添加一个UILable,并通过UITextView的代理方法监听其内容的改变,来判断UILable是否需要隐藏. 代码如下: 1 UITextView *textView = [[UITextView alloc] init];//创建一个textView 2 [s

UITextView实现PlaceHolder的方式

实现UITextView实现PlaceHolder的方式的方式有两种,这两种方法的核心就是通过通知来添加和去除PlaceHolder:下面来介绍两种方法:个人比较喜欢第一种,看起来更加合理. 方法1:原理是通过通知来改变PlaceHolder,把PlaceHolder看成是一个UILabel,设置UILabel的透明度,来让Placeholder显示与不显示.这种方法对UITextView本身影响较小.学习自Fly_Elephant:<UITextView实现PlaceHolder的方式>这篇

UITextView添加Placeholder(swift)

UITextView添加Placeholder(swift) by 伍雪颖 添加UILabel并初始化 public let placeholderLabel: UILabel = UILabel() @IBInspectable public var placeholder: String = "" { didSet { placeholderLabel.text = placeholder } } @IBInspectable public var placeholderColor

Linux ${}字符窜截取的方法汇总

Linux 字符窜截取的方法汇总 1.命令汇总 ${target-string#*sub-string} ${target-string##*sub-string} ${target-string%sub-string*} ${target-string%%*sub-string*} ---------------------------------------------------------------------------- ${target-string:start-index:st

Python字典高级使用方法汇总

Python字典高级使用方法汇总 字典(dictionary)是python中的一种非常灵活和强大的数据结构,可以完成很多操作.本文总结了一些除了基本的初始化.赋值.取值之外的常用的字典使用方法. 字典基础参考: [1]:http://www.w3cschool.cc/python/python-dictionary.html [2]:http://www.111cn.net/phper/python/56355.htm [3]:http://skyfen.iteye.com/blog/5675

Android项目:proguard混淆之常见问题及解决方法汇总

1.使用proguardgui混淆器对jar包进行混淆,出现EXCEPTION FROM SIMULATION错误: [2014-07-08 14:29:55 - Test024_HouseBox_v02_jar] Dx  EXCEPTION FROM SIMULATION: [2014-07-08 14:29:55 - Test024_HouseBox_v02_jar] Dx local variable type mismatch: attempt to set or access a va

转发:C#加密方法汇总

转自:C#加密方法汇总 方法一: 1 //须添加对System.Web的引用 2 using System.Web.Security; 3 ... 4 /// <summary> 5 /// SHA1加密字符串 6 /// </summary> 7 /// <param name="source">源字符串</param> 8 /// <returns>加密后的字符串</returns> 9 public stri