UITextView 和 UITextField 的提示信息placeholder
来源:互联网 发布:小女孩连裤袜淘宝 编辑:程序博客网 时间:2024/05/31 18:44
一、UITextFiled和UITextView很像,区别是前一个是只显示一行文本(即使打了回车,可只是显示一行),后一个可显示多行文本。两个的delegate方法也很像.
但是UITextFiled有一个属性placeholder,即文本框的提示信息。而UITextView则没有。一般当点击文本框的时候都需要将默认的提示信息去掉,将光标移动到开始位置。
对于UITextField 可以在delegate方法中这样设置:
1 - (void) textFiledDidBeginEditing:(UITextFiled*)textFiled 2 { 3 textFiled.placholder = nil; 4 [textFiled becomeFirstResponser]; 5 } 6 7 -(void)textFiledDidEndEditing:(UITextFiled*)textFiled 8 { 9 if(textFiled.text isEqualToString:@“”) || [textFiled.text stringBytrimmingCharactersInset:[NSCharacterSet whitespaceAndNewlineCharacterSet]]length] == 010 {11 [textFiled setText:@""];12 textFiled.placeholder = @"请您输入电话号码"; 13 } 14 [textFiled resignFirstResponder];15 }
但是对于UITextView则没有placeholder这个属性,可以直接设置textView.text = @"请您输入电话号码". 清除默认的text有几种方法
1.在UITextView上添加一个UILabel,再在-(void)textviewDidChanged:(UITextView*)textView方法中移除掉这个Label,[label removeFromSuperView];
2.继承UITextView,在drawRect中添加或者删除placeholder: 参考http://stackoverflow.com/questions/1328638/placeholder-in-uitextview
SSTextView.h//// SSTextView.h// SSToolkit//// Created by Sam Soffes on 8/18/10.// Copyright 2010-2011 Sam Soffes. All rights reserved.///** UITextView subclass that adds placeholder support like UITextField has. */@interface SSTextView : UITextView/** The string that is displayed when there is no other text in the text view. The default value is `nil`. */@property (nonatomic, retain) NSString *placeholder;/** The color of the placeholder. The default is `[UIColor lightGrayColor]`. */@property (nonatomic, retain) UIColor *placeholderColor;@endSSTextView.m//// SSTextView.m// SSToolkit//// Created by Sam Soffes on 8/18/10.// Copyright 2010-2011 Sam Soffes. All rights reserved.//#import "SSTextView.h"@interface SSTextView ()- (void)_initialize;- (void)_updateShouldDrawPlaceholder;- (void)_textChanged:(NSNotification *)notification;@end@implementation SSTextView { BOOL _shouldDrawPlaceholder;}#pragma mark - Accessors@synthesize placeholder = _placeholder;@synthesize placeholderColor = _placeholderColor;- (void)setText:(NSString *)string { [super setText:string]; [self _updateShouldDrawPlaceholder];}- (void)setPlaceholder:(NSString *)string { if ([string isEqual:_placeholder]) { return; } [_placeholder release]; _placeholder = [string retain]; [self _updateShouldDrawPlaceholder];}#pragma mark - NSObject- (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidChangeNotification object:self]; [_placeholder release]; [_placeholderColor release]; [super dealloc];}#pragma mark - UIView- (id)initWithCoder:(NSCoder *)aDecoder { if ((self = [super initWithCoder:aDecoder])) { [self _initialize]; } return self;}- (id)initWithFrame:(CGRect)frame { if ((self = [super initWithFrame:frame])) { [self _initialize]; } return self;}- (void)drawRect:(CGRect)rect { [super drawRect:rect]; if (_shouldDrawPlaceholder) { [_placeholderColor set]; [_placeholder drawInRect:CGRectMake(8.0f, 8.0f, self.frame.size.width - 16.0f, self.frame.size.height - 16.0f) withFont:self.font]; }}#pragma mark - Private- (void)_initialize {[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_textChanged:) name:UITextViewTextDidChangeNotification object:self]; self.placeholderColor = [UIColor colorWithWhite:0.702f alpha:1.0f]; _shouldDrawPlaceholder = NO;}- (void)_updateShouldDrawPlaceholder { BOOL prev = _shouldDrawPlaceholder; _shouldDrawPlaceholder = self.placeholder && self.placeholderColor && self.text.length == 0; if (prev != _shouldDrawPlaceholder) { [self setNeedsDisplay]; }}- (void)_textChanged:(NSNotification *)notificaiton { [self _updateShouldDrawPlaceholder]; }@end
3.还是使用UITextView的delegate方法
- (BOOL) textViewShouldBeginEditing:(UITextView *)textView{ if(textView.tag == 0) { textView.text = @""; textView.textColor = [UIColor blackColor]; textView.tag = 1; } return YES;}- (void)textViewDidChange:(UITextView *)textView{ if([textView.text length] == 0) { textView.text = @"Foobar placeholder"; textView.textColor = [UIColor lightGrayColor]; textView.tag = 0; }}
二、如果希望一进入Controller页面,就显示键盘,可以在viewWillAppear或者viewDidAppear中 添加[textFiled becomeFirstResponser]
- UITextView 和 UITextField 的提示信息placeholder
- UITextView 和 UITextField 的提示信息placeholder
- 关于UITextField和UITextView的placeholder
- Swift UITextField/UITextView(placeholder的制作)
- UITextView和UITextField的placeholder,键盘隐藏,键盘换行变完成字样
- UITextView-placeholder的实现和解析
- iOS修改UITextField的提示语(placeholder)的字体大小和颜色
- 设置UITextField和UITextView的光标颜色
- UITextView和UITextField的使用小技巧
- UITextView和UITextField退出键盘的方式
- iOS UITextView和UITextField的键盘处理
- UITextField修改placeholder的大小和颜色
- UITextView 的placeholder
- UITextView 的placeholder
- UITextView的placeholder属性
- UITextView的placeholder属性
- UITextView的placeholder
- UITextView 和 UITextField
- A. Domino piling
- 大侠们 谁用 C#做过 USB指令打印的?
- java设计模式总结篇--行为型模式(2)
- 非模态窗口模拟模态窗口
- 代码适当的重复,也有好处
- UITextView 和 UITextField 的提示信息placeholder
- 多文档DOC/VIEW框架不创建新文档
- Oracle PGA 管理
- fedora9 中qt-xll ernel/qt_x11.h:65:22: error: X11/Xlib.h: No such file or directory 问题解决
- 当前
- 关于ELF格式
- ios应用程序的启动流程with or without storyboard
- 加入收藏与设为首页代码(兼容各种浏览器)
- 查看so文件,readelf