为UITextField添加placeholder属性
来源:互联网 发布:网络彩票2018重启时间 编辑:程序博客网 时间:2024/03/29 13:33
在UITextField中自带placeholder属性,可以用于提示输入框信息。但是UITextView并不具备此功能
介绍两种方法来实现:
第一种:
初始化UITextView
- //首先定义UITextView
- UITextView *textView = [[UITextView alloc] init];
- textView.font = [UIFont systemFontOfSize:14];
- textView.frame =CGRectMake(10, 0, cell.contentView.bounds.size.width-20, side);
- textView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
- textView.backgroundColor = [UIColor whiteColor];
- [cell.contentView addSubview:textView];
- textView.hidden = NO;
- textView.delegate = self;
- //其次在UITextView上面覆盖个UILable,UILable设置为全局变量。
- uilabel.frame =CGRectMake(17, 8, cell.contentView.bounds.size.width - side+10, 20);
- uilabel.text = @"请填写审批意见...";
- uilabel.enabled = NO;//lable必须设置为不可用
- uilabel.backgroundColor = [UIColor clearColor];
- [cell.contentView addSubview:uilabel];
实现UITextView的代理
- -(void)textViewDidChange:(UITextView *)textView
- {
- self.examineText = textView.text;
- if (textView.text.length == 0) {
- uilabel.text = @"请填写审批意见...";
- }else{
- uilabel.text = @"";
- }
- }
第二种:
UITextView 实现 placeholder 及隐藏键盘
#import <Foundation/Foundation.h>
@interface UIPlaceHolderTextView : 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
#import "UIPlaceHolderTextView.h"
@implementation UIPlaceHolderTextView
@synthesize placeHolderLabel;
@synthesize placeholder;
@synthesize placeholderColor;
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[placeHolderLabel release]; 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] == 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];
}
@end
//隐藏键盘,实现UITextViewDelegate
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text
{
if ([text isEqualToString:@"\n"]) {
[m_textView resignFirstResponder];
return NO;
}
return YES;
}
- 为UITextField添加placeholder属性
- 为form表单元素添加placeholder属性
- UITextField属性--PlaceHolder颜色自定义
- UITextField属性--PlaceHolder颜色自定义
- UITextField属性--PlaceHolder颜色自定义
- UITextView添加placeholder属性
- UITextView添加placeholder属性
- TextView添加placeholder属性
- UITextView添加placeholder属性
- UITextField 关于placeholder属性的用法
- UITextfield placeholder
- 改变UITextField placeHolder颜色、字体以及添加icon
- 为 UITextField UITextView 添加toolBar
- 如何给TextView添加placeholder属性
- 如何给div添加placeholder属性
- 为label或者textView添加placeHolder
- UITextField修改 placeholder颜色
- UITextField设置placeholder颜色
- MSSQL_全角转半角函数
- 认识HTML5的WebSocket
- FZU2150 Fire Game(搜索题:bfs)
- How zygote and render process was created in Chromium
- mac 下修改host文件
- 为UITextField添加placeholder属性
- PG_全角转半角函数
- 用instancetype代替id作为返回值
- ab压力测试.
- 怎样避免(F5)刷新页面造成的重复提交
- java 导出csv文件
- 响应式Web设计的9项基本原则
- BAT脚本编写教程
- 开发:随笔记录之 OSGI的jar添加几个小问题及其注意的地方