首页 技术 正文
技术 2022年11月23日
0 收藏 351 点赞 5,064 浏览 5155 个字

控件UITextField有个placeholder属性,UITextField和UITextView使用方法基本类似,有两个小区别:1.UITextField单行输入,而UITextView可以多行输入。2.UITextField有placeholder属性,而UITextView没有。至于两者的代理方法,原理基本差不多,只是方法名略有差异。

实现该功能有两种方式 一种是 ①使用通知 显示隐藏遮盖物

②使用代理 给文本框重新赋值

1.在创建textView的时候,赋值其文本属性

即textView.text = @”想说的话”;

2.在开始编辑和结束编辑的代理方法中进行判断

 //
// ViewController.m
// 绘制TextView
//
// Created by zjj on 15/7/1.
// Copyright (c) 2015年 zjj. All rights reserved.
// #import "ViewController.h"
#import "DJTextView.h"
#import "DJplaceHolderTextView.h"
@interface ViewController () <UITextViewDelegate>
@property (nonatomic,strong)DJplaceHolderTextView *placeHolderText;
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
UITextField *text1 = [[UITextField alloc]initWithFrame:CGRectMake(, , , )];
text1.backgroundColor = [UIColor grayColor];
text1.placeholder = @"请输入账号";
text1.clearButtonMode = UITextFieldViewModeWhileEditing;
[self.view addSubview:text1]; // 做法① 使用通知 用一个UIlabel遮盖再UItextView上面 根据输入文字是否为0来隐藏显示遮盖物 文字提示
DJTextView *text = [[DJTextView alloc]initWithFrame:CGRectMake(, , , )];
text.backgroundColor = [UIColor grayColor];
text.placeholder = @"请输入账号";
text.placeholderColor = [UIColor whiteColor];
[self.view addSubview:text]; // 做法② 使用UITextViewDelegate代理 开始编辑和结束编辑事件重新赋值文本属性
_placeHolderText = [[DJplaceHolderTextView alloc]initWithFrame:CGRectMake(, , , )];
_placeHolderText.backgroundColor = [UIColor grayColor];
_placeHolderText.placeholder = @"请输入账号";
_placeHolderText.text = _placeHolderText.placeholder;
_placeHolderText.delegate = self;
[self.view addSubview:_placeHolderText];
}
/**
* 开始编辑事件
*/
- (void)textViewDidBeginEditing:(UITextView *)textView
{
// NSLog(@"textViewDidBeginEditing%@ - %ld - %@",textView.text,textView.text.length,self.placeHolderText.placeholder);
if ([textView.text isEqualToString:self.placeHolderText.placeholder]) {
textView.text = @"";
}
}
/**
* 结束编辑事件
*/
- (void)textViewDidEndEditing:(UITextView *)textView
{
// NSLog(@"textViewDidBeginEditing%@ - %ld - %@",textView.text,textView.text.length,self.placeHolderText.placeholder);
if (textView.text.length < ) {
textView.text = self.placeHolderText.placeholder;
}
} @end
 // 做法② 使用UITextViewDelegate代理 开始编辑和结束编辑事件重新赋值文本属性
 //
// DJplaceHolderTextView.h
// 绘制TextView
//
// Created by zjj on 15/7/2.
// Copyright (c) 2015年 zjj. All rights reserved.
// #import <UIKit/UIKit.h> @interface DJplaceHolderTextView : UITextView
/**
* placehold 用户输入前文本提示
*/
@property (nonatomic,copy) NSString *placeholder;
@end
// 做法① 使用通知 用一个UIlabel遮盖再UItextView上面 根据输入文字是否为0来隐藏显示遮盖物 文字提示
 //
// DJTextView.h
// 绘制TextView
//
// Created by zjj on 15/7/1.
// Copyright (c) 2015年 zjj. All rights reserved.
// #import <UIKit/UIKit.h>
/**
* UITextView 实现 placeholder 及隐藏键盘
*/
@interface DJTextView : UITextView
/**
* placehold 用户输入前文本提示
*/
@property (nonatomic,copy) NSString *placeholder;
/**
* placeholder 文字颜色
*/
@property (nonatomic,strong)UIColor *placeholderColor;
/**
* placeholder 提示label
*/
@property (nonatomic,strong)UILabel *placeholderLabel;
/**
* 文本改变事件
*/
-(void)textChanged:(NSNotification*)notification;
@end
 //
// DJTextView.m
// 绘制TextView
//
// Created by zjj on 15/7/1.
// Copyright (c) 2015年 zjj. All rights reserved.
// #import "DJTextView.h" @implementation DJTextView //-(void)setPlaceholder:(NSString *)placeholder
//{
// _placeholder = placeholder;
//
// [self setNeedsDisplay];
//}
//
//- (void)drawRect:(CGRect)rect
//{
//
// [self.placeholder drawInRect:rect withAttributes:nil];
//} - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; // placeHolderLabel = nil;
//
// [placeholderColor release]; placeholderColor = nil;
//
// [placeholder release]; placeholder = nil; // [super dealloc]; } - (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] == ) { return; } if([[self text] length] == ) { [[self viewWithTag:] setAlpha:]; } else { [[self viewWithTag:] setAlpha:]; } } - (void)setText:(NSString *)text { [super setText:text]; [self textChanged:nil]; } - (void)drawRect:(CGRect)rect { if( [[self placeholder] length] > ) { if ( self.placeholderLabel == nil ) { self.placeholderLabel = [[UILabel alloc] initWithFrame:CGRectMake(,,self.bounds.size.width - ,)];
self.placeholderLabel.lineBreakMode = UILineBreakModeWordWrap;//过时的
self.placeholderLabel.numberOfLines = ; self.placeholderLabel.font = self.font; self.placeholderLabel.backgroundColor = [UIColor clearColor]; self.placeholderLabel.textColor = self.placeholderColor; self.placeholderLabel.alpha = ; self.placeholderLabel.tag = ; [self addSubview:self.placeholderLabel]; } self.placeholderLabel.text = self.placeholder; [self.placeholderLabel sizeToFit]; [self sendSubviewToBack:self.placeholderLabel]; } if( [[self text] length] == && [[self placeholder] length] > ) { [[self viewWithTag:] setAlpha:]; } [super drawRect:rect]; }
////隐藏键盘,实现UITextViewDelegate
//
//-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text
//
//{
//
// if ([text isEqualToString:@"\n"]) {
//
// [textView resignFirstResponder];
//
// return NO;
//
// }
//
// return YES;
//
//} @end

推荐使用第一种

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,918
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,444
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,255
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,069
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,701
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,741