UITextView占位符

方法一:

  直接在textView上添加label,然后用 textViewDidChange:监听textView的文字长度,根据textView的长度等于0或者大于零来隐藏和显示Label

#pragma mark - textView占位符1
-(void)setupTextView{

    //定义UITextView
    UITextView *textView = [[UITextView alloc] init];
    textView.font = [UIFont systemFontOfSize:14];
    textView.frame =CGRectMake(10, 60, 300, 40);
    textView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    textView.backgroundColor = [UIColor whriteColor];
    _textView = textView;
    [self.view addSubview:textView];
    textView.delegate = self;

    //在UITextView上面覆盖个UILable,UILable设置为全局变量。
    UILabel *label = [[UILabel alloc]init];
    label.frame =CGRectMake(10, 5, 280, 20);
    label.text = @"请输入手机号码...";
    label.enabled = NO;//lable必须设置为不可用
    label.backgroundColor = [UIColor clearColor];
    _label = label;
    [textView addSubview:label];
}
//实现UITextView的代理
-(void)textViewDidChange:(UITextView *)textView
{
    if (textView.text.length == 0) {
        _label.text = @"请输入手机号码...";
    }else{
        _label.text = @"";
    }
}

方法二:

自定义 WXPlaceHolderTextView继承自UITextView

//
//  WXPlaceHolderTextView.h
//  test
//
//  Created by wangxu on 16/8/1.
//  Copyright © 2016年 wangxu. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface WXPlaceHolderTextView : UITextView {
    NSString *placeholder;
    UIColor *placeholderColor;

@private
    UILabel *placeHolderLabel;
}

@property(nonatomic, retain) UILabel *placeHolderLabel;
@property(nonatomic, retain) NSString *placeholder;
@property(nonatomic, retain) UIColor *placeholderColor;

-(void)textChanged:(NSNotification*)notification;

@end
//
//  WXPlaceHolderTextView.m
//  test
//
//  Created by wangxu on 16/8/1.
//  Copyright © 2016年 wangxu. All rights reserved.
//

#import "WXPlaceHolderTextView.h"

@implementation WXPlaceHolderTextView

@synthesize placeHolderLabel;
@synthesize placeholder;
@synthesize placeholderColor;

- (void)awakeFromNib
{
    [super awakeFromNib];
    [self setPlaceholder:@""];
    [self setPlaceholderColor:[UIColor lightGrayColor]];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil];
}

- (id)initWithFrame:(CGRect)frame
{
    if( (self = [super initWithFrame:frame]) )
    {
        [self setPlaceholder:@""];
        [self setPlaceholderColor:[UIColor lightGrayColor]];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil];
    }
    return self;
}

- (void)textChanged:(NSNotification *)notification
{
    if([[self placeholder] length] == 0)
    {
        return;
    }

    if([[self text] length] == 0)
    {
        [[self viewWithTag:999] setAlpha:1];
    }
    else
    {
        [[self viewWithTag:999] setAlpha:0];
    }
}

- (void)setText:(NSString *)text {
    [super setText:text];
    [self textChanged:nil];
}

- (void)drawRect:(CGRect)rect
{
    if( [[self placeholder] length] > 0 )
    {
        if ( placeHolderLabel == nil )
        {
            placeHolderLabel = [[UILabel alloc] initWithFrame:CGRectMake(8,8,self.bounds.size.width - 16,0)];
            placeHolderLabel.lineBreakMode = UILineBreakModeWordWrap;
            placeHolderLabel.numberOfLines = 0;
            placeHolderLabel.font = self.font;
            placeHolderLabel.backgroundColor = [UIColor clearColor];
            placeHolderLabel.textColor = self.placeholderColor;
            placeHolderLabel.alpha = 0;
            placeHolderLabel.tag = 999;
            [self addSubview:placeHolderLabel];
        }

        placeHolderLabel.text = self.placeholder;
        [placeHolderLabel sizeToFit];
        [self sendSubviewToBack:placeHolderLabel];
    }

    if( [[self text] length] == 0 && [[self placeholder] length] > 0 )
    {
        [[self viewWithTag:999] setAlpha:1];
    }

    [super drawRect:rect];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end

使用:

#pragma mark - textView占位符2
-(void)setupWXTextView{

    //首先定义UITextView
    WXPlaceHolderTextView *textView = [[WXPlaceHolderTextView alloc] init];
    textView.font = [UIFont systemFontOfSize:14];
    textView.frame =CGRectMake(10, 60, 300, 40);
    textView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    textView.backgroundColor = [UIColor whiteColor];
    _mytextView = textView;
    //设置textView的placeholder的文字和字体颜色
    textView.placeholder = @"真好用~ ~ ~";
    textView.placeholderColor = [UIColor redColor];
    [self.view addSubview:textView];
    textView.hidden = NO;
    textView.delegate = self;
}

//隐藏键盘,实现UITextViewDelegate
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text
{
    if ([text isEqualToString:@"\n"]) {
        [_textView resignFirstResponder];
        return NO;
    }
    return YES;
}
时间: 2024-08-01 05:22:21

UITextView占位符的相关文章

基于UITextView的UITextField功能效果(有占位符功能)

前言 首先我们在自定义UITextView的同时,我们要弄清楚UITextView的UITextField两者的区别,哪些是UITextView有的功能而UITextField没有,或者UITextField有的功能而UITextView没有.接下来我们来看看他们各自的特性以及他们之间的区别. 一 UITextField与UITextView区别 UITextVIew在View的属性方面比UITextField灵活. 最重要的是UITextFiled可以设置InputText的相关属性. UIT

UITextView加占位符placeholder

@property (nonatomic ,retain) UITextView * contentText; @property (nonatomic ,retain) NSString * placeholderText; - (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view. [email protected]"这一刻的想法..."; _c

打造一个支持占位符的多行文本输入框

前言 众所周知,在iOS开发中,UIKit提供的文本输入框有两种:UITextField和UITextView.前者支持站位付,但不支持多行输入,后者支持多行输入,但不支持占位符.而在实际开发过程中经常须要使用到既能支持多行输入,又能支持占位符的文本输入框,这就必须自己实现了.我在须要使用的时候也在网上找了一些别人写的,但是,说真的,没找到好用的,并且看他们的源码实现过程还较为复杂.所以就自己写了一个. 开发思路 这是一个很简单的东西,就不在此详细介绍实现的过程,代码已经上传到github,支持

占位符

占位符 说明 %s 字符串(采用str()的显示) %r 字符串(采用repr()的显示) %c 单个字符 %b 二进制整数 %d 十进制整数 %i 十进制整数 %o 八进制整数 %x 十六进制整数 %e 指数 (基底写为e) %E 指数 (基底写为E) %f 浮点数 %F 浮点数,与上相同 %g 指数(e)或浮点数 (根据显示长度) %G 指数(E)或浮点数 (根据显示长度)

Android之使用JAVA占位符格式数据(很实用)

小编虽然是学java出生,但工作之后就一直从事android开发,很多java基础都忘记完了,最近一年从ES换到了AS,原来的很多习惯都收到了挑战,比如我喜欢ES写方法的时候先在JAVA project中写个main方法验证下对不对,比如数据拆分,时间对比等等,但AS使用中,因为和ES很多环境变量冲突,就把ES卸载了,现在很多都不方便. 在开发中使用比较多的就是设置view的值/给view填充值得时候,需要格式化数据,小编今天用了一写时间,做了下小小的整理 1.%s 字符串类型 用处:多用于字符

UITextField 占位符的颜色及光标颜色(及如何学习新知识)

一. 设置光标的颜色 1.如何是xib或storyBoard在设置控件属性找有没有有关颜色的属性(找到了backgroundColor,TextColor,还有一个是tintColor),这样就排除了前两个,试下tintColor,果然有效. 2.如何是用代码写的,可以在UITextField中找相关color,如果没有我们要的属性在父类里找或父类的父类中找,这时找到了这个tintColor. 二.设置点位符的颜色 方法1: 1.因为上面已经找过了没有其它跟颜色相关的属性了,这时我们在UITex

8 -- 深入使用Spring -- 1...4 属性占位符配置器

8.1.4 属性占位符配置器 PropertyPlaceholderConfigurer 是一个容器后处理器,负责读取Properties属性文件里的属性值,并将这些属性值设置成Spring配置文件的数据. 通过使用PropertyPlaceholderConfigurer后处理器,可以将Spring配置文件中的部分数据放在属性文件中设置. XML : <?xml version="1.0" encoding="UTF-8"?> <!-- Spri

SpringMVC(七) RequestMapping 路径中带占位符的URL

使用方法:在@RequestMapping("/delete/{id}")中,通过{id}带入pathvariable,然后在方法中,通过@PathVariable("变量名称") Iteger id 的方式引入占位符. 控制器代码: package com.tiekui.springmvc.handlers; import org.springframework.stereotype.Controller; import org.springframework.w

国际化之MessageFormat与占位符

如果一个字符串文本中包含了多个与国际化相关的数据,可以使用MessageFormat类对这些数据进行批量处理. 例如: 在2016年1月9日的时候,一场台风导致了500间房屋的摧毁和¥1000000元人民币的损失. 在上面这句话中,包含了时间.数字和货币等多个与国家化相关的数据,已经用下划线标出,而我们可以使用MessageFormat对这句话中多个数据进行批量处理. 要想使用MessageFormat进行批量处理,就要使用到占位符,用占位符替换上面的数据(不需要写死的数据或者与国际化相关的数据